blob: f3953800da004b16eb158b4fa69c2688ea15c009 [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 Petkov50308cd2011-06-01 18:25:07 -070019
Darin Petkove7cb7f82011-06-03 13:21:51 -070020using std::string;
21using std::vector;
22
Darin Petkov50308cd2011-06-01 18:25:07 -070023namespace shill {
24
Darin Petkove7cb7f82011-06-03 13:21:51 -070025const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
26const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
27const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
28const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
29const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
30const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
31const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
32const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Darin Petkovd1b715b2011-06-02 21:21:22 -070033const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Darin Petkov92c43902011-06-09 20:46:06 -070034const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease";
35const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070036const char DHCPConfig::kReasonBound[] = "BOUND";
37const char DHCPConfig::kReasonFail[] = "FAIL";
38const char DHCPConfig::kReasonRebind[] = "REBIND";
39const char DHCPConfig::kReasonReboot[] = "REBOOT";
40const char DHCPConfig::kReasonRenew[] = "RENEW";
41
Darin Petkove7cb7f82011-06-03 13:21:51 -070042
Darin Petkovf7897bc2011-06-08 17:13:36 -070043DHCPConfig::DHCPConfig(DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070044 const string &device_name,
Darin Petkov3258a812011-06-23 11:28:45 -070045 GLib *glib)
Darin Petkovf65e9282011-06-21 14:29:56 -070046 : IPConfig(device_name),
Darin Petkovd1b715b2011-06-02 21:21:22 -070047 provider_(provider),
Darin Petkovf7897bc2011-06-08 17:13:36 -070048 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070049 child_watch_tag_(0),
50 root_("/"),
Darin Petkovf7897bc2011-06-08 17:13:36 -070051 glib_(glib) {
Chris Masone27c4aa52011-07-02 13:10:14 -070052 store_.RegisterConstString(flimflam::kAddressProperty,
53 &(properties().address));
Darin Petkovf65e9282011-06-21 14:29:56 -070054 VLOG(2) << __func__ << ": " << device_name;
Darin Petkov50308cd2011-06-01 18:25:07 -070055}
56
57DHCPConfig::~DHCPConfig() {
Darin Petkovf65e9282011-06-21 14:29:56 -070058 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070059
60 // Don't leave behind dhcpcd running.
61 Stop();
62
Darin Petkov98dd6a02011-06-10 15:12:57 -070063 // Make sure we don't get any callbacks to the destroyed instance.
Darin Petkov92c43902011-06-09 20:46:06 -070064 CleanupClientState();
Darin Petkovd1b715b2011-06-02 21:21:22 -070065}
66
Darin Petkov92c43902011-06-09 20:46:06 -070067bool DHCPConfig::RequestIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070068 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070069 if (!pid_) {
70 return Start();
71 }
72 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -070073 LOG(ERROR) << "Unable to request IP before acquiring destination.";
74 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -070075 }
Darin Petkov92c43902011-06-09 20:46:06 -070076 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -070077}
78
Darin Petkov92c43902011-06-09 20:46:06 -070079bool DHCPConfig::RenewIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070080 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070081 if (!pid_) {
82 return false;
83 }
84 if (!proxy_.get()) {
85 LOG(ERROR) << "Unable to renew IP before acquiring destination.";
Darin Petkovd1b715b2011-06-02 21:21:22 -070086 return false;
87 }
Darin Petkovaceede32011-07-18 15:32:38 -070088 proxy_->Rebind(device_name());
Darin Petkovd1b715b2011-06-02 21:21:22 -070089 return true;
90}
91
Darin Petkov92c43902011-06-09 20:46:06 -070092bool DHCPConfig::ReleaseIP() {
Darin Petkovf65e9282011-06-21 14:29:56 -070093 VLOG(2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -070094 if (!pid_) {
95 return true;
96 }
97 if (!proxy_.get()) {
98 LOG(ERROR) << "Unable to release IP before acquiring destination.";
99 return false;
100 }
Darin Petkovaceede32011-07-18 15:32:38 -0700101 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700102 Stop();
103 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700104}
105
Darin Petkovaceede32011-07-18 15:32:38 -0700106void DHCPConfig::InitProxy(const char *service) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700107 if (!proxy_.get()) {
Darin Petkovaceede32011-07-18 15:32:38 -0700108 proxy_.reset(ProxyFactory::factory()->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700109 }
110}
111
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700112void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700113 const Configuration &configuration) {
114 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700115 if (reason == kReasonFail) {
116 LOG(ERROR) << "Received failure event from DHCP client.";
117 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700118 return;
119 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700120 if (reason != kReasonBound &&
121 reason != kReasonRebind &&
122 reason != kReasonReboot &&
123 reason != kReasonRenew) {
124 LOG(WARNING) << "Event ignored.";
125 return;
126 }
127 IPConfig::Properties properties;
128 CHECK(ParseConfiguration(configuration, &properties));
129 UpdateProperties(properties, true);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700130}
131
Darin Petkovd1b715b2011-06-02 21:21:22 -0700132bool DHCPConfig::Start() {
Darin Petkovf65e9282011-06-21 14:29:56 -0700133 VLOG(2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700134
Darin Petkov98dd6a02011-06-10 15:12:57 -0700135 char *argv[4] = {
136 const_cast<char *>(kDHCPCDPath),
137 const_cast<char *>("-B"), // foreground
Darin Petkovf65e9282011-06-21 14:29:56 -0700138 const_cast<char *>(device_name().c_str()),
Darin Petkov98dd6a02011-06-10 15:12:57 -0700139 NULL
140 };
141 char *envp[1] = { NULL };
Darin Petkovd1b715b2011-06-02 21:21:22 -0700142
Darin Petkov98dd6a02011-06-10 15:12:57 -0700143 CHECK(!pid_);
Darin Petkovf7897bc2011-06-08 17:13:36 -0700144 if (!glib_->SpawnAsync(NULL,
145 argv,
146 envp,
147 G_SPAWN_DO_NOT_REAP_CHILD,
148 NULL,
149 NULL,
Darin Petkov98dd6a02011-06-10 15:12:57 -0700150 &pid_,
Darin Petkovf7897bc2011-06-08 17:13:36 -0700151 NULL)) {
Darin Petkovd1b715b2011-06-02 21:21:22 -0700152 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath;
153 return false;
154 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700155 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700156 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700157 CHECK(!child_watch_tag_);
158 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700159 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700160}
161
Darin Petkov92c43902011-06-09 20:46:06 -0700162void DHCPConfig::Stop() {
163 if (pid_) {
164 VLOG(2) << "Terminating " << pid_;
165 PLOG_IF(ERROR, kill(pid_, SIGTERM) < 0);
166 }
167}
168
Darin Petkov98dd6a02011-06-10 15:12:57 -0700169bool DHCPConfig::Restart() {
170 // Check to ensure that this instance doesn't get destroyed in the middle of
171 // this call. If stopping a running client while there's only one reference to
172 // this instance, we will end up destroying it when the PID is unbound from
173 // the Provider. Since the Provider doesn't invoke Restart, this would mean
174 // that Restart was erroneously executed through a bare reference.
175 CHECK(!pid_ || !HasOneRef());
176 Stop();
177 if (pid_) {
178 provider_->UnbindPID(pid_);
179 }
180 CleanupClientState();
181 return Start();
182}
183
Darin Petkove7cb7f82011-06-03 13:21:51 -0700184string DHCPConfig::GetIPv4AddressString(unsigned int address) {
185 char str[INET_ADDRSTRLEN];
186 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
187 return str;
188 }
189 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
190 return "";
191}
192
193bool DHCPConfig::ParseConfiguration(const Configuration& configuration,
194 IPConfig::Properties *properties) {
195 VLOG(2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700196 properties->method = flimflam::kTypeDHCP;
Paul Stewart1d18e8c2011-07-15 11:00:31 -0700197 properties->address_family = IPAddress::kAddressFamilyIPv4;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700198 for (Configuration::const_iterator it = configuration.begin();
199 it != configuration.end(); ++it) {
200 const string &key = it->first;
201 const DBus::Variant &value = it->second;
202 VLOG(2) << "Processing key: " << key;
203 if (key == kConfigurationKeyIPAddress) {
204 properties->address = GetIPv4AddressString(value.reader().get_uint32());
205 if (properties->address.empty()) {
206 return false;
207 }
208 } else if (key == kConfigurationKeySubnetCIDR) {
209 properties->subnet_cidr = value.reader().get_byte();
210 } else if (key == kConfigurationKeyBroadcastAddress) {
211 properties->broadcast_address =
212 GetIPv4AddressString(value.reader().get_uint32());
213 if (properties->broadcast_address.empty()) {
214 return false;
215 }
216 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700217 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700218 if (routers.empty()) {
219 LOG(ERROR) << "No routers provided.";
220 return false;
221 }
222 properties->gateway = GetIPv4AddressString(routers[0]);
223 if (properties->gateway.empty()) {
224 return false;
225 }
226 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700227 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700228 for (vector<unsigned int>::const_iterator it = servers.begin();
229 it != servers.end(); ++it) {
230 string server = GetIPv4AddressString(*it);
231 if (server.empty()) {
232 return false;
233 }
234 properties->dns_servers.push_back(server);
235 }
236 } else if (key == kConfigurationKeyDomainName) {
237 properties->domain_name = value.reader().get_string();
238 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700239 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700240 } else if (key == kConfigurationKeyMTU) {
241 int mtu = value.reader().get_uint16();
242 if (mtu >= 576) {
243 properties->mtu = mtu;
244 }
245 } else {
246 VLOG(2) << "Key ignored.";
247 }
248 }
249 return true;
250}
251
Darin Petkov92c43902011-06-09 20:46:06 -0700252void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
253 VLOG(2) << "pid " << pid << " exit status " << status;
254 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
255 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700256 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700257 config->CleanupClientState();
258
259 // |config| instance may be destroyed after this call.
260 config->provider_->UnbindPID(pid);
261}
262
263void DHCPConfig::CleanupClientState() {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700264 if (child_watch_tag_) {
265 glib_->SourceRemove(child_watch_tag_);
266 child_watch_tag_ = 0;
267 }
268 if (pid_) {
269 glib_->SpawnClosePID(pid_);
270 pid_ = 0;
271 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700272 proxy_.reset();
Darin Petkov92c43902011-06-09 20:46:06 -0700273 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease,
Darin Petkovf65e9282011-06-21 14:29:56 -0700274 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700275 false);
276 file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID,
Darin Petkovf65e9282011-06-21 14:29:56 -0700277 device_name().c_str())),
Darin Petkov92c43902011-06-09 20:46:06 -0700278 false);
279}
280
Darin Petkov50308cd2011-06-01 18:25:07 -0700281} // namespace shill