blob: 581528b435ad0d0078e3b458faaa6383e9191ecf [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"
Darin Petkov3258a812011-06-23 11:28:45 -070016#include "shill/glib.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070017#include "shill/ip_address.h"
Darin Petkovaceede32011-07-18 15:32:38 -070018#include "shill/proxy_factory.h"
Darin Petkova7b89492011-07-27 12:48:17 -070019#include "shill/shill_event.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,
Darin Petkov3258a812011-06-23 11:28:45 -070051 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070052 : IPConfig(control_interface, device_name, kType),
Darin Petkovd1b715b2011-06-02 21:21:22 -070053 provider_(provider),
Darin Petkovf7897bc2011-06-08 17:13:36 -070054 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070055 child_watch_tag_(0),
56 root_("/"),
Darin Petkova7b89492011-07-27 12:48:17 -070057 task_factory_(this),
58 dispatcher_(dispatcher),
Darin Petkovf7897bc2011-06-08 17:13:36 -070059 glib_(glib) {
Paul Stewartac4ac002011-08-26 12:04:26 -070060 store()->RegisterConstString(flimflam::kAddressProperty,
61 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070062 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070063}
64
65DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070066 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070067
68 // Don't leave behind dhcpcd running.
69 Stop();
70
Darin Petkov98dd6a02011-06-10 15:12:57 -070071 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070072 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070073}
74
Darin Petkov92c43902011-06-09 20:46:06 -070075bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070076 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070077 if (!pid_) {
78 return Start();
79 }
80 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070081 LOG(ERROR) << "Unable to request IP before acquiring destination.";
82 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070083 }
Darin Petkov92c43902011-06-09 20:46:06 -070084 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070085}
86
Darin Petkov92c43902011-06-09 20:46:06 -070087bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070088 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070089 if (!pid_) {
90 return false;
91 }
Darin Petkovaceede32011-07-18 15:32:38 -070092 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070093 return true;
94}
95
Darin Petkov92c43902011-06-09 20:46:06 -070096bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070097 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070098 if (!pid_) {
99 return true;
100 }
Darin Petkova7b89492011-07-27 12:48:17 -0700101 if (proxy_.get()) {
102 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700103 }
Darin Petkov98dd6a02011-06-10 15:12:57 -0700104 Stop();
105 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700106}
107
Darin Petkova7b89492011-07-27 12:48:17 -0700108void DHCPConfig::InitProxy(const string &service) {
109 // Defer proxy creation because dbus-c++ doesn't allow registration of new
110 // D-Bus objects in the context of a D-Bus signal handler.
Darin Petkovd1b715b2011-06-02 21:21:22 -0700111 if (!proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700112 dispatcher_->PostTask(
113 task_factory_.NewRunnableMethod(&DHCPConfig::InitProxyTask, service));
114 }
115}
116
117void DHCPConfig::InitProxyTask(const string &service) {
118 if (!proxy_.get()) {
119 VLOG(2) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovaceede32011-07-18 15:32:38 -0700120 proxy_.reset(ProxyFactory::factory()->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700121 }
122}
123
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700124void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700125 const Configuration &configuration) {
126 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700127 if (reason == kReasonFail) {
128 LOG(ERROR) << "Received failure event from DHCP client.";
129 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700130 return;
131 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700132 if (reason != kReasonBound &&
133 reason != kReasonRebind &&
134 reason != kReasonReboot &&
135 reason != kReasonRenew) {
136 LOG(WARNING) << "Event ignored.";
137 return;
138 }
139 IPConfig::Properties properties;
140 CHECK(ParseConfiguration(configuration, &properties));
141 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700142}
143
Darin Petkovd1b715b2011-06-02 21:21:22 -0700144bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700145 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700146
Darin Petkov98dd6a02011-06-10 15:12:57 -0700147 char *argv[4] = {
148 const_cast<char *>(kDHCPCDPath),
149 const_cast<char *>("-B"), // foreground
Darin Petkovf65e9282011-06-21 14:29:56 -0700150 const_cast<char *>(device_name().c_str()),
Darin Petkov98dd6a02011-06-10 15:12:57 -0700151 NULL
152 };
153 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,
157 argv,
158 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_;
177 PLOG_IF(ERROR, kill(pid_, SIGTERM) < 0);
178 }
179}
180
Darin Petkov98dd6a02011-06-10 15:12:57 -0700181bool DHCPConfig::Restart() {
182 // Check to ensure that this instance doesn't get destroyed in the middle of
183 // this call. If stopping a running client while there's only one reference to
184 // this instance, we will end up destroying it when the PID is unbound from
185 // the Provider. Since the Provider doesn't invoke Restart, this would mean
186 // that Restart was erroneously executed through a bare reference.
187 CHECK(!pid_ || !HasOneRef());
188 Stop();
189 if (pid_) {
190 provider_->UnbindPID(pid_);
191 }
192 CleanupClientState();
193 return Start();
194}
195
Darin Petkove7cb7f82011-06-03 13:21:51 -0700196string DHCPConfig::GetIPv4AddressString(unsigned int address) {
197 char str[INET_ADDRSTRLEN];
198 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
199 return str;
200 }
201 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
202 return "";
203}
204
205bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
206 IPConfig::Properties *properties) {
207 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700208 properties->method = flimflam::kTypeDHCP;
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700209 properties->address_family = IPAddress::kAddressFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700210 for (Configuration::const_iterator it = configuration.begin();
211 it != configuration.end(); ++it) {
212 const string &key = it->first;
213 const DBus::Variant &value = it->second;
214 VLOG(2) << "Processing key: " << key;
215 if (key == kConfigurationKeyIPAddress) {
216 properties->address = GetIPv4AddressString(value.reader().get_uint32());
217 if (properties->address.empty()) {
218 return false;
219 }
220 } else if (key == kConfigurationKeySubnetCIDR) {
221 properties->subnet_cidr = value.reader().get_byte();
222 } else if (key == kConfigurationKeyBroadcastAddress) {
223 properties->broadcast_address =
224 GetIPv4AddressString(value.reader().get_uint32());
225 if (properties->broadcast_address.empty()) {
226 return false;
227 }
228 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700229 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700230 if (routers.empty()) {
231 LOG(ERROR) << "No routers provided.";
232 return false;
233 }
234 properties->gateway = GetIPv4AddressString(routers[0]);
235 if (properties->gateway.empty()) {
236 return false;
237 }
238 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700239 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700240 for (vector<unsigned int>::const_iterator it = servers.begin();
241 it != servers.end(); ++it) {
242 string server = GetIPv4AddressString(*it);
243 if (server.empty()) {
244 return false;
245 }
246 properties->dns_servers.push_back(server);
247 }
248 } else if (key == kConfigurationKeyDomainName) {
249 properties->domain_name = value.reader().get_string();
250 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700251 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700252 } else if (key == kConfigurationKeyMTU) {
253 int mtu = value.reader().get_uint16();
254 if (mtu >= 576) {
255 properties->mtu = mtu;
256 }
257 } else {
258 VLOG(2) << "Key ignored.";
259 }
260 }
261 return true;
262}
263
Darin Petkov92c43902011-06-09 20:46:06 -0700264void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
265 VLOG(2) << "pid " << pid << " exit status " << status;
266 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
267 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700268 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700269 config->CleanupClientState();
270
271 // |config| instance may be destroyed after this call.
272 config->provider_->UnbindPID(pid);
273}
274
275void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700276 if (child_watch_tag_) {
277 glib_->SourceRemove(child_watch_tag_);
278 child_watch_tag_ = 0;
279 }
280 if (pid_) {
281 glib_->SpawnClosePID(pid_);
282 pid_ = 0;
283 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700284 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700285 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700286 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700287 false);
288 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700289 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700290 false);
291}
292
Darin Petkov50308cd2011-06-01 18:25:07 -0700293} // namespace shill