blob: 3acefc0dfe1cb8ff0e3d1dbd9134eed60d45cb85 [file] [log] [blame]
Darin Petkov50308cd2011-06-01 18:25:07 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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>
8
Darin Petkov92c43902011-06-09 20:46:06 -07009#include <base/file_util.h>
Darin Petkov50308cd2011-06-01 18:25:07 -070010#include <base/logging.h>
Darin Petkov92c43902011-06-09 20:46:06 -070011#include <base/stringprintf.h>
Chris Masone43b48a12011-07-01 13:37:07 -070012#include <chromeos/dbus/service_constants.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070013
14#include "shill/dhcpcd_proxy.h"
15#include "shill/dhcp_provider.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Darin Petkov3258a812011-06-23 11:28:45 -070017#include "shill/glib.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070018#include "shill/ip_address.h"
Darin Petkovaceede32011-07-18 15:32:38 -070019#include "shill/proxy_factory.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070020
Darin Petkove7cb7f82011-06-03 13:21:51 -070021using std::string;
22using std::vector;
23
Darin Petkov50308cd2011-06-01 18:25:07 -070024namespace shill {
25
Chris Masone0756f232011-07-21 17:24:00 -070026// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070027const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
28const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
29const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
30const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
31const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
32const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
33const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
34const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Darin Petkovd1b715b2011-06-02 21:21:22 -070035const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Darin Petkov92c43902011-06-09 20:46:06 -070036const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease";
37const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070038const char DHCPConfig::kReasonBound[] = "BOUND";
39const char DHCPConfig::kReasonFail[] = "FAIL";
40const char DHCPConfig::kReasonRebind[] = "REBIND";
41const char DHCPConfig::kReasonReboot[] = "REBOOT";
42const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070043// static
44const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070045
Darin Petkove7cb7f82011-06-03 13:21:51 -070046
Chris Masone19e30402011-07-19 15:48:47 -070047DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070048 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070049 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070050 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080051 const string &request_hostname,
Darin Petkov3258a812011-06-23 11:28:45 -070052 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070053 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070054 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070055 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080056 request_hostname_(request_hostname),
Darin Petkovf7897bc2011-06-08 17:13:36 -070057 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070058 child_watch_tag_(0),
59 root_("/"),
Darin Petkova7b89492011-07-27 12:48:17 -070060 task_factory_(this),
61 dispatcher_(dispatcher),
Darin Petkovf7897bc2011-06-08 17:13:36 -070062 glib_(glib) {
mukesh agrawalde29fa82011-09-16 16:16:36 -070063 mutable_store()->RegisterConstString(flimflam::kAddressProperty,
64 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070065 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070066}
67
68DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070069 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070070
71 // Don't leave behind dhcpcd running.
72 Stop();
73
Darin Petkov98dd6a02011-06-10 15:12:57 -070074 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070075 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070076}
77
Darin Petkov92c43902011-06-09 20:46:06 -070078bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070079 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070080 if (!pid_) {
81 return Start();
82 }
83 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070084 LOG(ERROR) << "Unable to request IP before acquiring destination.";
85 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070086 }
Darin Petkov92c43902011-06-09 20:46:06 -070087 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070088}
89
Darin Petkov92c43902011-06-09 20:46:06 -070090bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070091 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070092 if (!pid_) {
93 return false;
94 }
Darin Petkovaceede32011-07-18 15:32:38 -070095 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070096 return true;
97}
98
Darin Petkov92c43902011-06-09 20:46:06 -070099bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700100 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700101 if (!pid_) {
102 return true;
103 }
Darin Petkova7b89492011-07-27 12:48:17 -0700104 if (proxy_.get()) {
105 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700106 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700107 Stop();
108 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700109}
110
Darin Petkova7b89492011-07-27 12:48:17 -0700111void DHCPConfig::InitProxy(const string &service) {
112 // Defer proxy creation because dbus-c++ doesn't allow registration of new
113 // D-Bus objects in the context of a D-Bus signal handler.
Darin Petkovd1b715b2011-06-02 21:21:22 -0700114 if (!proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700115 dispatcher_->PostTask(
116 task_factory_.NewRunnableMethod(&DHCPConfig::InitProxyTask, service));
117 }
118}
119
120void DHCPConfig::InitProxyTask(const string &service) {
121 if (!proxy_.get()) {
122 VLOG(2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700123 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700124 }
125}
126
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700127void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700128 const Configuration &configuration) {
129 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700130 if (reason == kReasonFail) {
131 LOG(ERROR) << "Received failure event from DHCP client.";
132 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700133 return;
134 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700135 if (reason != kReasonBound &&
136 reason != kReasonRebind &&
137 reason != kReasonReboot &&
138 reason != kReasonRenew) {
139 LOG(WARNING) << "Event ignored.";
140 return;
141 }
142 IPConfig::Properties properties;
143 CHECK(ParseConfiguration(configuration, &properties));
144 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700145}
146
Darin Petkovd1b715b2011-06-02 21:21:22 -0700147bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700148 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700149
Paul Stewartd32f4842012-01-11 16:08:13 -0800150 vector<char *> args;
151 args.push_back(const_cast<char *>(kDHCPCDPath));
152 args.push_back(const_cast<char *>("-B")); // foreground
153 args.push_back(const_cast<char *>(device_name().c_str()));
154 if (!request_hostname_.empty()) {
155 args.push_back(const_cast<char *>("-h")); // request hostname
156 args.push_back(const_cast<char *>(request_hostname_.c_str()));
157 }
158 args.push_back(NULL);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700159 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700160
Darin Petkov98dd6a02011-06-10 15:12:57 -0700161 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700162 if (!glib_->SpawnAsync(NULL,
Paul Stewartd32f4842012-01-11 16:08:13 -0800163 args.data(),
Darin Petkovf7897bc2011-06-08 17:13:36 -0700164 envp,
165 G_SPAWN_DO_NOT_REAP_CHILD,
166 NULL,
167 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700168 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700169 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700170 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
171 return false;
172 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700173 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700174 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700175 CHECK(!child_watch_tag_);
176 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700177 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700178}
179
Darin Petkov92c43902011-06-09 20:46:06 -0700180void DHCPConfig::Stop() {
181 if (pid_) {
182 VLOG(2) << "Terminating " << pid_;
183 PLOG_IF(ERROR, kill(pid_, SIGTERM) < 0);
184 }
185}
186
Darin Petkov98dd6a02011-06-10 15:12:57 -0700187bool DHCPConfig::Restart() {
188 // Check to ensure that this instance doesn't get destroyed in the middle of
189 // this call. If stopping a running client while there's only one reference to
190 // this instance, we will end up destroying it when the PID is unbound from
191 // the Provider. Since the Provider doesn't invoke Restart, this would mean
192 // that Restart was erroneously executed through a bare reference.
193 CHECK(!pid_ || !HasOneRef());
194 Stop();
195 if (pid_) {
196 provider_->UnbindPID(pid_);
197 }
198 CleanupClientState();
199 return Start();
200}
201
Darin Petkove7cb7f82011-06-03 13:21:51 -0700202string DHCPConfig::GetIPv4AddressString(unsigned int address) {
203 char str[INET_ADDRSTRLEN];
204 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
205 return str;
206 }
207 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
208 return "";
209}
210
211bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
212 IPConfig::Properties *properties) {
213 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700214 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700215 properties->address_family = IPAddress::kFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700216 for (Configuration::const_iterator it = configuration.begin();
217 it != configuration.end(); ++it) {
218 const string &key = it->first;
219 const DBus::Variant &value = it->second;
220 VLOG(2) << "Processing key: " << key;
221 if (key == kConfigurationKeyIPAddress) {
222 properties->address = GetIPv4AddressString(value.reader().get_uint32());
223 if (properties->address.empty()) {
224 return false;
225 }
226 } else if (key == kConfigurationKeySubnetCIDR) {
227 properties->subnet_cidr = value.reader().get_byte();
228 } else if (key == kConfigurationKeyBroadcastAddress) {
229 properties->broadcast_address =
230 GetIPv4AddressString(value.reader().get_uint32());
231 if (properties->broadcast_address.empty()) {
232 return false;
233 }
234 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700235 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700236 if (routers.empty()) {
237 LOG(ERROR) << "No routers provided.";
238 return false;
239 }
240 properties->gateway = GetIPv4AddressString(routers[0]);
241 if (properties->gateway.empty()) {
242 return false;
243 }
244 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700245 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700246 for (vector<unsigned int>::const_iterator it = servers.begin();
247 it != servers.end(); ++it) {
248 string server = GetIPv4AddressString(*it);
249 if (server.empty()) {
250 return false;
251 }
252 properties->dns_servers.push_back(server);
253 }
254 } else if (key == kConfigurationKeyDomainName) {
255 properties->domain_name = value.reader().get_string();
256 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700257 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700258 } else if (key == kConfigurationKeyMTU) {
259 int mtu = value.reader().get_uint16();
260 if (mtu >= 576) {
261 properties->mtu = mtu;
262 }
263 } else {
264 VLOG(2) << "Key ignored.";
265 }
266 }
267 return true;
268}
269
Darin Petkov92c43902011-06-09 20:46:06 -0700270void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
271 VLOG(2) << "pid " << pid << " exit status " << status;
272 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
273 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700274 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700275 config->CleanupClientState();
276
277 // |config| instance may be destroyed after this call.
278 config->provider_->UnbindPID(pid);
279}
280
281void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700282 if (child_watch_tag_) {
283 glib_->SourceRemove(child_watch_tag_);
284 child_watch_tag_ = 0;
285 }
286 if (pid_) {
287 glib_->SpawnClosePID(pid_);
288 pid_ = 0;
289 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700290 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700291 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700292 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700293 false);
294 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700295 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700296 false);
297}
298
Darin Petkov50308cd2011-06-01 18:25:07 -0700299} // namespace shill