blob: 4742baa417d2c3a056fe2f08df9725b1f7ae3a21 [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>
mukesh agrawal6c6655d2012-12-06 14:49:50 -08008#include <stdlib.h>
Thieu Le94eed562012-02-21 15:57:29 -08009#include <sys/wait.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070010
Darin Petkov92c43902011-06-09 20:46:06 -070011#include <base/file_util.h>
Paul Stewart65bcd082012-11-16 09:37:14 -080012#include <base/string_split.h>
Darin Petkov92c43902011-06-09 20:46:06 -070013#include <base/stringprintf.h>
Chris Masone43b48a12011-07-01 13:37:07 -070014#include <chromeos/dbus/service_constants.h>
Darin Petkovd1b715b2011-06-02 21:21:22 -070015
16#include "shill/dhcpcd_proxy.h"
17#include "shill/dhcp_provider.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070018#include "shill/event_dispatcher.h"
Darin Petkov3258a812011-06-23 11:28:45 -070019#include "shill/glib.h"
Paul Stewart1d18e8c2011-07-15 11:00:31 -070020#include "shill/ip_address.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070021#include "shill/logging.h"
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070022#include "shill/minijail.h"
Darin Petkovaceede32011-07-18 15:32:38 -070023#include "shill/proxy_factory.h"
Darin Petkov50308cd2011-06-01 18:25:07 -070024
Darin Petkove7cb7f82011-06-03 13:21:51 -070025using std::string;
26using std::vector;
27
Darin Petkov50308cd2011-06-01 18:25:07 -070028namespace shill {
29
Chris Masone0756f232011-07-21 17:24:00 -070030// static
Darin Petkove7cb7f82011-06-03 13:21:51 -070031const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress";
Paul Stewart65bcd082012-11-16 09:37:14 -080032const char DHCPConfig::kConfigurationKeyClasslessStaticRoutes[] =
33 "ClasslessStaticRoutes";
Darin Petkove7cb7f82011-06-03 13:21:51 -070034const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers";
35const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName";
36const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch";
37const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress";
38const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU";
39const char DHCPConfig::kConfigurationKeyRouters[] = "Routers";
40const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR";
Thieu Le94eed562012-02-21 15:57:29 -080041const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50;
42const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000;
Darin Petkovd1b715b2011-06-02 21:21:22 -070043const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd";
Jorge Lucangeli Obes2f3169d2012-04-25 11:38:25 -070044const char DHCPConfig::kDHCPCDPathFormatPID[] =
45 "var/run/dhcpcd/dhcpcd-%s.pid";
mukesh agrawalcc0fded2012-05-09 13:40:58 -070046const int DHCPConfig::kDHCPTimeoutSeconds = 30;
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070047const char DHCPConfig::kDHCPCDUser[] = "dhcp";
Darin Petkov14c29ec2012-03-02 11:34:19 +010048const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070049const char DHCPConfig::kReasonBound[] = "BOUND";
50const char DHCPConfig::kReasonFail[] = "FAIL";
Paul Stewarta02ee492012-05-16 10:04:53 -070051const char DHCPConfig::kReasonGatewayArp[] = "GATEWAY-ARP";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070052const char DHCPConfig::kReasonRebind[] = "REBIND";
53const char DHCPConfig::kReasonReboot[] = "REBOOT";
54const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070055// static
56const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070057
Darin Petkove7cb7f82011-06-03 13:21:51 -070058
Chris Masone19e30402011-07-19 15:48:47 -070059DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070060 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070061 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070062 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080063 const string &request_hostname,
Paul Stewartd408fdf2012-05-07 17:15:57 -070064 const string &lease_file_suffix,
65 bool arp_gateway,
Darin Petkov3258a812011-06-23 11:28:45 -070066 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070067 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070068 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070069 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080070 request_hostname_(request_hostname),
Paul Stewartd408fdf2012-05-07 17:15:57 -070071 lease_file_suffix_(lease_file_suffix),
72 arp_gateway_(arp_gateway),
Darin Petkovf7897bc2011-06-08 17:13:36 -070073 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070074 child_watch_tag_(0),
Paul Stewart217c61d2013-06-13 15:12:02 -070075 is_lease_active_(false),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070076 lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds),
Darin Petkov92c43902011-06-09 20:46:06 -070077 root_("/"),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070078 weak_ptr_factory_(this),
Darin Petkova7b89492011-07-27 12:48:17 -070079 dispatcher_(dispatcher),
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070080 glib_(glib),
81 minijail_(Minijail::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070083 if (lease_file_suffix_.empty()) {
84 lease_file_suffix_ = device_name;
85 }
Darin Petkov50308cd2011-06-01 18:25:07 -070086}
87
88DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070089 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070090
91 // Don't leave behind dhcpcd running.
mukesh agrawal1835e772013-01-15 18:35:03 -080092 Stop(__func__);
Darin Petkovd1b715b2011-06-02 21:21:22 -070093}
94
Darin Petkov92c43902011-06-09 20:46:06 -070095bool DHCPConfig::RequestIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070096 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070097 if (!pid_) {
98 return Start();
99 }
100 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700101 LOG(ERROR) << "Unable to request IP before acquiring destination.";
102 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700103 }
Darin Petkov92c43902011-06-09 20:46:06 -0700104 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700105}
106
Darin Petkov92c43902011-06-09 20:46:06 -0700107bool DHCPConfig::RenewIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700108 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700109 if (!pid_) {
110 return false;
111 }
Paul Stewartc02344a2012-08-17 16:57:37 -0700112 if (!proxy_.get()) {
113 LOG(ERROR) << "Unable to renew IP before acquiring destination.";
114 return false;
115 }
Darin Petkovaceede32011-07-18 15:32:38 -0700116 proxy_->Rebind(device_name());
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700117 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700118 return true;
119}
120
Paul Stewart217c61d2013-06-13 15:12:02 -0700121bool DHCPConfig::ReleaseIP(ReleaseReason reason) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700122 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700123 if (!pid_) {
124 return true;
125 }
Paul Stewart217c61d2013-06-13 15:12:02 -0700126
127 // If we are using static IP and haven't retrieved a lease yet, we should
128 // allow the DHCP process to continue until we have a lease.
129 if (!is_lease_active_ && reason == IPConfig::kReleaseReasonStaticIP) {
130 return true;
131 }
132
Paul Stewarta02ee492012-05-16 10:04:53 -0700133 // If we are using gateway unicast ARP to speed up re-connect, don't
134 // give up our leases when we disconnect.
Paul Stewart217c61d2013-06-13 15:12:02 -0700135 bool should_keep_lease =
136 reason == IPConfig::kReleaseReasonDisconnect && arp_gateway_;
137
138 if (!should_keep_lease && proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700139 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700140 }
mukesh agrawal1835e772013-01-15 18:35:03 -0800141 Stop(__func__);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700142 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700143}
144
Darin Petkova7b89492011-07-27 12:48:17 -0700145void DHCPConfig::InitProxy(const string &service) {
Darin Petkova7b89492011-07-27 12:48:17 -0700146 if (!proxy_.get()) {
Christopher Wiley0236e762012-11-16 14:11:05 -0800147 LOG(INFO) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700148 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700149 }
150}
151
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700152void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700153 const Configuration &configuration) {
154 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700155 if (reason == kReasonFail) {
156 LOG(ERROR) << "Received failure event from DHCP client.";
157 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700158 return;
159 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700160 if (reason != kReasonBound &&
161 reason != kReasonRebind &&
162 reason != kReasonReboot &&
Paul Stewarta02ee492012-05-16 10:04:53 -0700163 reason != kReasonRenew &&
164 reason != kReasonGatewayArp) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700165 LOG(WARNING) << "Event ignored.";
166 return;
167 }
168 IPConfig::Properties properties;
169 CHECK(ParseConfiguration(configuration, &properties));
Paul Stewart217c61d2013-06-13 15:12:02 -0700170
171 // This needs to be set before calling UpdateProperties() below since
172 // those functions may indirectly call other methods like ReleaseIP that
173 // depend on or change this value.
174 is_lease_active_ = true;
175
Paul Stewarta02ee492012-05-16 10:04:53 -0700176 if (reason == kReasonGatewayArp) {
177 // This is a non-authoritative confirmation that we or on the same
178 // network as the one we received a lease on previously. The DHCP
179 // client is still running, so we should not cancel the timeout
180 // until that completes. In the meantime, however, we can tentatively
181 // configure our network in anticipation of successful completion.
182 IPConfig::UpdateProperties(properties, true);
183 } else {
184 UpdateProperties(properties, true);
185 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700186}
187
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700188void DHCPConfig::UpdateProperties(const Properties &properties, bool success) {
189 StopDHCPTimeout();
190 IPConfig::UpdateProperties(properties, success);
191}
192
Darin Petkovd1b715b2011-06-02 21:21:22 -0700193bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700194 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700195
mukesh agrawalae30e9e2013-05-28 14:09:16 -0700196 // TODO(quiche): This should be migrated to use ExternalTask.
197 // (crbug.com/246263).
Paul Stewartd32f4842012-01-11 16:08:13 -0800198 vector<char *> args;
199 args.push_back(const_cast<char *>(kDHCPCDPath));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700200 args.push_back(const_cast<char *>("-B")); // Run in foreground.
mukesh agrawal7eb02892012-05-29 11:22:37 -0700201 args.push_back(const_cast<char *>("-q")); // Only warnings+errors to stderr.
Paul Stewartd32f4842012-01-11 16:08:13 -0800202 if (!request_hostname_.empty()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700203 args.push_back(const_cast<char *>("-h")); // Request hostname from server.
Paul Stewartd32f4842012-01-11 16:08:13 -0800204 args.push_back(const_cast<char *>(request_hostname_.c_str()));
205 }
Paul Stewartd408fdf2012-05-07 17:15:57 -0700206 if (arp_gateway_) {
207 args.push_back(const_cast<char *>("-R")); // ARP for default gateway.
Paul Stewarta02ee492012-05-16 10:04:53 -0700208 args.push_back(const_cast<char *>("-U")); // Enable unicast ARP on renew.
Paul Stewartd408fdf2012-05-07 17:15:57 -0700209 }
210 string interface_arg(device_name());
211 if (lease_file_suffix_ != device_name()) {
212 interface_arg = base::StringPrintf("%s=%s", device_name().c_str(),
213 lease_file_suffix_.c_str());
214 }
215 args.push_back(const_cast<char *>(interface_arg.c_str()));
Paul Stewartd32f4842012-01-11 16:08:13 -0800216 args.push_back(NULL);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700217
218 struct minijail *jail = minijail_->New();
219 minijail_->DropRoot(jail, kDHCPCDUser);
220 minijail_->UseCapabilities(jail,
221 CAP_TO_MASK(CAP_NET_BIND_SERVICE) |
222 CAP_TO_MASK(CAP_NET_BROADCAST) |
223 CAP_TO_MASK(CAP_NET_ADMIN) |
224 CAP_TO_MASK(CAP_NET_RAW));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700225
Darin Petkov98dd6a02011-06-10 15:12:57 -0700226 CHECK(!pid_);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700227 if (!minijail_->RunAndDestroy(jail, args, &pid_)) {
228 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail.";
Darin Petkovd1b715b2011-06-02 21:21:22 -0700229 return false;
230 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700231 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700232 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700233 CHECK(!child_watch_tag_);
234 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700235 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700236 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700237}
238
mukesh agrawal1835e772013-01-15 18:35:03 -0800239void DHCPConfig::Stop(const char *reason) {
Darin Petkov3fe17662013-02-04 14:19:08 +0100240 LOG_IF(INFO, pid_) << "Stopping " << pid_ << " (" << reason << ")";
241 KillClient();
242 // KillClient waits for the client to terminate so it's safe to cleanup the
243 // state.
244 CleanupClientState();
245}
246
247void DHCPConfig::KillClient() {
248 if (!pid_) {
249 return;
Darin Petkov92c43902011-06-09 20:46:06 -0700250 }
Darin Petkov3fe17662013-02-04 14:19:08 +0100251 if (kill(pid_, SIGTERM) < 0) {
252 PLOG(ERROR);
253 return;
254 }
255 pid_t ret;
256 int num_iterations =
257 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
258 for (int count = 0; count < num_iterations; ++count) {
259 ret = waitpid(pid_, NULL, WNOHANG);
260 if (ret == pid_ || ret == -1)
261 break;
262 usleep(kDHCPCDExitPollMilliseconds * 1000);
263 if (count == num_iterations / 2) {
264 // Make one last attempt to kill dhcpcd.
265 LOG(WARNING) << "Terminating " << pid_ << " with SIGKILL.";
266 kill(pid_, SIGKILL);
267 }
268 }
269 if (ret != pid_)
270 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700271}
272
Darin Petkov98dd6a02011-06-10 15:12:57 -0700273bool DHCPConfig::Restart() {
Darin Petkov3fe17662013-02-04 14:19:08 +0100274 // Take a reference of this instance to make sure we don't get destroyed in
275 // the middle of this call.
276 DHCPConfigRefPtr me = this;
277 me->Stop(__func__);
278 return me->Start();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700279}
280
mukesh agrawal28fd88e2013-05-02 14:06:10 -0700281// static
Darin Petkove7cb7f82011-06-03 13:21:51 -0700282string DHCPConfig::GetIPv4AddressString(unsigned int address) {
283 char str[INET_ADDRSTRLEN];
284 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
285 return str;
286 }
287 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
288 return "";
289}
290
Paul Stewart65bcd082012-11-16 09:37:14 -0800291// static
292bool DHCPConfig::ParseClasslessStaticRoutes(const string &classless_routes,
293 IPConfig::Properties *properties) {
294 if (classless_routes.empty()) {
295 // It is not an error for this string to be empty.
296 return true;
297 }
298
299 vector<string> route_strings;
300 base::SplitString(classless_routes, ' ', &route_strings);
301 if (route_strings.size() % 2) {
302 LOG(ERROR) << "In " << __func__ << ": Size of route_strings array "
303 << "is a non-even number: " << route_strings.size();
304 return false;
305 }
306
307 vector<IPConfig::Route> routes;
308 vector<string>::iterator route_iterator = route_strings.begin();
309 // Classless routes are a space-delimited array of
310 // "destination/prefix gateway" values. As such, we iterate twice
311 // for each pass of the loop below.
312 while (route_iterator != route_strings.end()) {
313 const string &destination_as_string(*route_iterator);
314 route_iterator++;
315 IPAddress destination(IPAddress::kFamilyIPv4);
316 if (!destination.SetAddressAndPrefixFromString(
317 destination_as_string)) {
318 LOG(ERROR) << "In " << __func__ << ": Expected an IP address/prefix "
319 << "but got an unparsable: " << destination_as_string;
320 return false;
321 }
322
323 CHECK(route_iterator != route_strings.end());
324 const string &gateway_as_string(*route_iterator);
325 route_iterator++;
326 IPAddress gateway(IPAddress::kFamilyIPv4);
327 if (!gateway.SetAddressFromString(gateway_as_string)) {
328 LOG(ERROR) << "In " << __func__ << ": Expected a router IP address "
329 << "but got an unparsable: " << gateway_as_string;
330 return false;
331 }
332
333 if (destination.prefix() == 0 && properties->gateway.empty()) {
334 // If a default route is provided in the classless parameters and
335 // we don't already have one, apply this as the default route.
336 SLOG(DHCP, 2) << "In " << __func__ << ": Setting default gateway to "
337 << gateway_as_string;
338 CHECK(gateway.IntoString(&properties->gateway));
339 } else {
340 IPConfig::Route route;
341 CHECK(destination.IntoString(&route.host));
342 IPAddress netmask(IPAddress::GetAddressMaskFromPrefix(
343 destination.family(), destination.prefix()));
344 CHECK(netmask.IntoString(&route.netmask));
345 CHECK(gateway.IntoString(&route.gateway));
346 routes.push_back(route);
347 SLOG(DHCP, 2) << "In " << __func__ << ": Adding route to to "
348 << destination_as_string << " via " << gateway_as_string;
349 }
350 }
351
352 if (!routes.empty()) {
353 properties->routes.swap(routes);
354 }
355
356 return true;
357}
358
mukesh agrawal28fd88e2013-05-02 14:06:10 -0700359// static
Paul Stewart65bcd082012-11-16 09:37:14 -0800360bool DHCPConfig::ParseConfiguration(const Configuration &configuration,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700361 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700362 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700363 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700364 properties->address_family = IPAddress::kFamilyIPv4;
Paul Stewart65bcd082012-11-16 09:37:14 -0800365 string classless_static_routes;
366 bool default_gateway_parse_error = false;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700367 for (Configuration::const_iterator it = configuration.begin();
368 it != configuration.end(); ++it) {
369 const string &key = it->first;
370 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700371 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700372 if (key == kConfigurationKeyIPAddress) {
373 properties->address = GetIPv4AddressString(value.reader().get_uint32());
374 if (properties->address.empty()) {
375 return false;
376 }
377 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700378 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700379 } else if (key == kConfigurationKeyBroadcastAddress) {
380 properties->broadcast_address =
381 GetIPv4AddressString(value.reader().get_uint32());
382 if (properties->broadcast_address.empty()) {
383 return false;
384 }
385 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700386 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700387 if (routers.empty()) {
388 LOG(ERROR) << "No routers provided.";
Paul Stewart65bcd082012-11-16 09:37:14 -0800389 default_gateway_parse_error = true;
390 } else {
391 properties->gateway = GetIPv4AddressString(routers[0]);
392 if (properties->gateway.empty()) {
393 LOG(ERROR) << "Failed to parse router parameter provided.";
394 default_gateway_parse_error = true;
395 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700396 }
397 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700398 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700399 for (vector<unsigned int>::const_iterator it = servers.begin();
400 it != servers.end(); ++it) {
401 string server = GetIPv4AddressString(*it);
402 if (server.empty()) {
403 return false;
404 }
405 properties->dns_servers.push_back(server);
406 }
407 } else if (key == kConfigurationKeyDomainName) {
408 properties->domain_name = value.reader().get_string();
409 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700410 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700411 } else if (key == kConfigurationKeyMTU) {
412 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100413 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700414 properties->mtu = mtu;
415 }
Paul Stewart65bcd082012-11-16 09:37:14 -0800416 } else if (key == kConfigurationKeyClasslessStaticRoutes) {
417 classless_static_routes = value.reader().get_string();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700418 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700419 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700420 }
421 }
Paul Stewart65bcd082012-11-16 09:37:14 -0800422 ParseClasslessStaticRoutes(classless_static_routes, properties);
423 if (default_gateway_parse_error && properties->gateway.empty()) {
424 return false;
425 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700426 return true;
427}
428
Darin Petkov92c43902011-06-09 20:46:06 -0700429void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
mukesh agrawal6c6655d2012-12-06 14:49:50 -0800430 if (status == EXIT_SUCCESS) {
431 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
432 } else {
433 LOG(WARNING) << "pid " << pid << " exit status " << status;
434 }
Darin Petkov92c43902011-06-09 20:46:06 -0700435 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
436 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700437 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700438 // |config| instance may be destroyed after this call.
Darin Petkov3fe17662013-02-04 14:19:08 +0100439 config->CleanupClientState();
Darin Petkov92c43902011-06-09 20:46:06 -0700440}
441
442void DHCPConfig::CleanupClientState() {
Darin Petkov3fe17662013-02-04 14:19:08 +0100443 SLOG(DHCP, 2) << __func__ << ": " << device_name();
444 StopDHCPTimeout();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700445 if (child_watch_tag_) {
446 glib_->SourceRemove(child_watch_tag_);
447 child_watch_tag_ = 0;
448 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700449 proxy_.reset();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700450 if (lease_file_suffix_ == device_name()) {
451 // If the lease file suffix was left as default, clean it up at exit.
452 file_util::Delete(root_.Append(
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800453 base::StringPrintf(DHCPProvider::kDHCPCDPathFormatLease,
Paul Stewartd408fdf2012-05-07 17:15:57 -0700454 device_name().c_str())), false);
455 }
456 file_util::Delete(root_.Append(
457 base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false);
Darin Petkov3fe17662013-02-04 14:19:08 +0100458 if (pid_) {
459 int pid = pid_;
460 pid_ = 0;
461 // |this| instance may be destroyed after this call.
462 provider_->UnbindPID(pid);
463 }
Paul Stewart217c61d2013-06-13 15:12:02 -0700464 is_lease_active_ = false;
Darin Petkov92c43902011-06-09 20:46:06 -0700465}
466
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700467void DHCPConfig::StartDHCPTimeout() {
468 lease_acquisition_timeout_callback_.Reset(
469 Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr()));
470 dispatcher_->PostDelayedTask(
471 lease_acquisition_timeout_callback_.callback(),
472 lease_acquisition_timeout_seconds_ * 1000);
473}
474
475void DHCPConfig::StopDHCPTimeout() {
476 lease_acquisition_timeout_callback_.Cancel();
477}
478
479void DHCPConfig::ProcessDHCPTimeout() {
480 LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " "
481 << "(after " << lease_acquisition_timeout_seconds_ << " seconds).";
482 UpdateProperties(IPConfig::Properties(), false);
483}
484
Darin Petkov50308cd2011-06-01 18:25:07 -0700485} // namespace shill