blob: b2b7da32138791c8e154eca6572a8e101f541c3a [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";
Paul Stewartd408fdf2012-05-07 17:15:57 -070044const char DHCPConfig::kDHCPCDPathFormatLease[] =
45 "var/lib/dhcpcd/dhcpcd-%s.lease";
Jorge Lucangeli Obes2f3169d2012-04-25 11:38:25 -070046const char DHCPConfig::kDHCPCDPathFormatPID[] =
47 "var/run/dhcpcd/dhcpcd-%s.pid";
mukesh agrawalcc0fded2012-05-09 13:40:58 -070048const int DHCPConfig::kDHCPTimeoutSeconds = 30;
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070049const char DHCPConfig::kDHCPCDUser[] = "dhcp";
Darin Petkov14c29ec2012-03-02 11:34:19 +010050const int DHCPConfig::kMinMTU = 576;
Darin Petkovf9b0ca82011-06-20 12:10:23 -070051const char DHCPConfig::kReasonBound[] = "BOUND";
52const char DHCPConfig::kReasonFail[] = "FAIL";
Paul Stewarta02ee492012-05-16 10:04:53 -070053const char DHCPConfig::kReasonGatewayArp[] = "GATEWAY-ARP";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070054const char DHCPConfig::kReasonRebind[] = "REBIND";
55const char DHCPConfig::kReasonReboot[] = "REBOOT";
56const char DHCPConfig::kReasonRenew[] = "RENEW";
Chris Masone0756f232011-07-21 17:24:00 -070057// static
58const char DHCPConfig::kType[] = "dhcp";
Darin Petkovf9b0ca82011-06-20 12:10:23 -070059
Darin Petkove7cb7f82011-06-03 13:21:51 -070060
Chris Masone19e30402011-07-19 15:48:47 -070061DHCPConfig::DHCPConfig(ControlInterface *control_interface,
Darin Petkova7b89492011-07-27 12:48:17 -070062 EventDispatcher *dispatcher,
Chris Masone19e30402011-07-19 15:48:47 -070063 DHCPProvider *provider,
Darin Petkovf65e9282011-06-21 14:29:56 -070064 const string &device_name,
Paul Stewartd32f4842012-01-11 16:08:13 -080065 const string &request_hostname,
Paul Stewartd408fdf2012-05-07 17:15:57 -070066 const string &lease_file_suffix,
67 bool arp_gateway,
Darin Petkov3258a812011-06-23 11:28:45 -070068 GLib *glib)
Chris Masone0756f232011-07-21 17:24:00 -070069 : IPConfig(control_interface, device_name, kType),
Darin Petkovab565bb2011-10-06 02:55:51 -070070 proxy_factory_(ProxyFactory::GetInstance()),
Darin Petkovd1b715b2011-06-02 21:21:22 -070071 provider_(provider),
Paul Stewartd32f4842012-01-11 16:08:13 -080072 request_hostname_(request_hostname),
Paul Stewartd408fdf2012-05-07 17:15:57 -070073 lease_file_suffix_(lease_file_suffix),
74 arp_gateway_(arp_gateway),
Darin Petkovf7897bc2011-06-08 17:13:36 -070075 pid_(0),
Darin Petkov92c43902011-06-09 20:46:06 -070076 child_watch_tag_(0),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070077 lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds),
Darin Petkov92c43902011-06-09 20:46:06 -070078 root_("/"),
mukesh agrawalcc0fded2012-05-09 13:40:58 -070079 weak_ptr_factory_(this),
Darin Petkova7b89492011-07-27 12:48:17 -070080 dispatcher_(dispatcher),
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -070081 glib_(glib),
82 minijail_(Minijail::GetInstance()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070083 SLOG(DHCP, 2) << __func__ << ": " << device_name;
Paul Stewartd408fdf2012-05-07 17:15:57 -070084 if (lease_file_suffix_.empty()) {
85 lease_file_suffix_ = device_name;
86 }
Darin Petkov50308cd2011-06-01 18:25:07 -070087}
88
89DHCPConfig::~DHCPConfig() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070090 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov92c43902011-06-09 20:46:06 -070091
92 // Don't leave behind dhcpcd running.
mukesh agrawal1835e772013-01-15 18:35:03 -080093 Stop(__func__);
Darin Petkovd1b715b2011-06-02 21:21:22 -070094}
95
Darin Petkov92c43902011-06-09 20:46:06 -070096bool DHCPConfig::RequestIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -070097 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -070098 if (!pid_) {
99 return Start();
100 }
101 if (!proxy_.get()) {
Darin Petkov98dd6a02011-06-10 15:12:57 -0700102 LOG(ERROR) << "Unable to request IP before acquiring destination.";
103 return Restart();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700104 }
Darin Petkov92c43902011-06-09 20:46:06 -0700105 return RenewIP();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700106}
107
Darin Petkov92c43902011-06-09 20:46:06 -0700108bool DHCPConfig::RenewIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700109 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700110 if (!pid_) {
111 return false;
112 }
Paul Stewartc02344a2012-08-17 16:57:37 -0700113 if (!proxy_.get()) {
114 LOG(ERROR) << "Unable to renew IP before acquiring destination.";
115 return false;
116 }
Darin Petkovaceede32011-07-18 15:32:38 -0700117 proxy_->Rebind(device_name());
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700118 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700119 return true;
120}
121
Darin Petkov92c43902011-06-09 20:46:06 -0700122bool DHCPConfig::ReleaseIP() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700123 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700124 if (!pid_) {
125 return true;
126 }
Paul Stewarta02ee492012-05-16 10:04:53 -0700127 // If we are using gateway unicast ARP to speed up re-connect, don't
128 // give up our leases when we disconnect.
129 if (!arp_gateway_ && proxy_.get()) {
Darin Petkova7b89492011-07-27 12:48:17 -0700130 proxy_->Release(device_name());
Darin Petkov98dd6a02011-06-10 15:12:57 -0700131 }
mukesh agrawal1835e772013-01-15 18:35:03 -0800132 Stop(__func__);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700133 return true;
Darin Petkov92c43902011-06-09 20:46:06 -0700134}
135
Darin Petkova7b89492011-07-27 12:48:17 -0700136void DHCPConfig::InitProxy(const string &service) {
Darin Petkova7b89492011-07-27 12:48:17 -0700137 if (!proxy_.get()) {
Christopher Wiley0236e762012-11-16 14:11:05 -0800138 LOG(INFO) << "Init DHCP Proxy: " << device_name() << " at " << service;
Darin Petkovab565bb2011-10-06 02:55:51 -0700139 proxy_.reset(proxy_factory_->CreateDHCPProxy(service));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700140 }
141}
142
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700143void DHCPConfig::ProcessEventSignal(const string &reason,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700144 const Configuration &configuration) {
145 LOG(INFO) << "Event reason: " << reason;
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700146 if (reason == kReasonFail) {
147 LOG(ERROR) << "Received failure event from DHCP client.";
148 UpdateProperties(IPConfig::Properties(), false);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700149 return;
150 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700151 if (reason != kReasonBound &&
152 reason != kReasonRebind &&
153 reason != kReasonReboot &&
Paul Stewarta02ee492012-05-16 10:04:53 -0700154 reason != kReasonRenew &&
155 reason != kReasonGatewayArp) {
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700156 LOG(WARNING) << "Event ignored.";
157 return;
158 }
159 IPConfig::Properties properties;
160 CHECK(ParseConfiguration(configuration, &properties));
Paul Stewarta02ee492012-05-16 10:04:53 -0700161 if (reason == kReasonGatewayArp) {
162 // This is a non-authoritative confirmation that we or on the same
163 // network as the one we received a lease on previously. The DHCP
164 // client is still running, so we should not cancel the timeout
165 // until that completes. In the meantime, however, we can tentatively
166 // configure our network in anticipation of successful completion.
167 IPConfig::UpdateProperties(properties, true);
168 } else {
169 UpdateProperties(properties, true);
170 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700171}
172
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700173void DHCPConfig::UpdateProperties(const Properties &properties, bool success) {
174 StopDHCPTimeout();
175 IPConfig::UpdateProperties(properties, success);
176}
177
Darin Petkovd1b715b2011-06-02 21:21:22 -0700178bool DHCPConfig::Start() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700179 SLOG(DHCP, 2) << __func__ << ": " << device_name();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700180
Paul Stewartd32f4842012-01-11 16:08:13 -0800181 vector<char *> args;
182 args.push_back(const_cast<char *>(kDHCPCDPath));
Paul Stewartd408fdf2012-05-07 17:15:57 -0700183 args.push_back(const_cast<char *>("-B")); // Run in foreground.
mukesh agrawal7eb02892012-05-29 11:22:37 -0700184 args.push_back(const_cast<char *>("-q")); // Only warnings+errors to stderr.
Paul Stewartd32f4842012-01-11 16:08:13 -0800185 if (!request_hostname_.empty()) {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700186 args.push_back(const_cast<char *>("-h")); // Request hostname from server.
Paul Stewartd32f4842012-01-11 16:08:13 -0800187 args.push_back(const_cast<char *>(request_hostname_.c_str()));
188 }
Paul Stewartd408fdf2012-05-07 17:15:57 -0700189 if (arp_gateway_) {
190 args.push_back(const_cast<char *>("-R")); // ARP for default gateway.
Paul Stewarta02ee492012-05-16 10:04:53 -0700191 args.push_back(const_cast<char *>("-U")); // Enable unicast ARP on renew.
Paul Stewartd408fdf2012-05-07 17:15:57 -0700192 }
193 string interface_arg(device_name());
194 if (lease_file_suffix_ != device_name()) {
195 interface_arg = base::StringPrintf("%s=%s", device_name().c_str(),
196 lease_file_suffix_.c_str());
197 }
198 args.push_back(const_cast<char *>(interface_arg.c_str()));
Paul Stewartd32f4842012-01-11 16:08:13 -0800199 args.push_back(NULL);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700200
201 struct minijail *jail = minijail_->New();
202 minijail_->DropRoot(jail, kDHCPCDUser);
203 minijail_->UseCapabilities(jail,
204 CAP_TO_MASK(CAP_NET_BIND_SERVICE) |
205 CAP_TO_MASK(CAP_NET_BROADCAST) |
206 CAP_TO_MASK(CAP_NET_ADMIN) |
207 CAP_TO_MASK(CAP_NET_RAW));
Darin Petkovd1b715b2011-06-02 21:21:22 -0700208
Darin Petkov98dd6a02011-06-10 15:12:57 -0700209 CHECK(!pid_);
Jorge Lucangeli Obesad43cc62012-04-11 16:25:43 -0700210 if (!minijail_->RunAndDestroy(jail, args, &pid_)) {
211 LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail.";
Darin Petkovd1b715b2011-06-02 21:21:22 -0700212 return false;
213 }
Darin Petkovd1b715b2011-06-02 21:21:22 -0700214 LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_;
Darin Petkovf7897bc2011-06-08 17:13:36 -0700215 provider_->BindPID(pid_, this);
Darin Petkov98dd6a02011-06-10 15:12:57 -0700216 CHECK(!child_watch_tag_);
217 child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this);
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700218 StartDHCPTimeout();
Darin Petkovd1b715b2011-06-02 21:21:22 -0700219 return true;
Darin Petkov50308cd2011-06-01 18:25:07 -0700220}
221
mukesh agrawal1835e772013-01-15 18:35:03 -0800222void DHCPConfig::Stop(const char *reason) {
Darin Petkov3fe17662013-02-04 14:19:08 +0100223 LOG_IF(INFO, pid_) << "Stopping " << pid_ << " (" << reason << ")";
224 KillClient();
225 // KillClient waits for the client to terminate so it's safe to cleanup the
226 // state.
227 CleanupClientState();
228}
229
230void DHCPConfig::KillClient() {
231 if (!pid_) {
232 return;
Darin Petkov92c43902011-06-09 20:46:06 -0700233 }
Darin Petkov3fe17662013-02-04 14:19:08 +0100234 if (kill(pid_, SIGTERM) < 0) {
235 PLOG(ERROR);
236 return;
237 }
238 pid_t ret;
239 int num_iterations =
240 kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds;
241 for (int count = 0; count < num_iterations; ++count) {
242 ret = waitpid(pid_, NULL, WNOHANG);
243 if (ret == pid_ || ret == -1)
244 break;
245 usleep(kDHCPCDExitPollMilliseconds * 1000);
246 if (count == num_iterations / 2) {
247 // Make one last attempt to kill dhcpcd.
248 LOG(WARNING) << "Terminating " << pid_ << " with SIGKILL.";
249 kill(pid_, SIGKILL);
250 }
251 }
252 if (ret != pid_)
253 PLOG(ERROR);
Darin Petkov92c43902011-06-09 20:46:06 -0700254}
255
Darin Petkov98dd6a02011-06-10 15:12:57 -0700256bool DHCPConfig::Restart() {
Darin Petkov3fe17662013-02-04 14:19:08 +0100257 // Take a reference of this instance to make sure we don't get destroyed in
258 // the middle of this call.
259 DHCPConfigRefPtr me = this;
260 me->Stop(__func__);
261 return me->Start();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700262}
263
Darin Petkove7cb7f82011-06-03 13:21:51 -0700264string DHCPConfig::GetIPv4AddressString(unsigned int address) {
265 char str[INET_ADDRSTRLEN];
266 if (inet_ntop(AF_INET, &address, str, arraysize(str))) {
267 return str;
268 }
269 LOG(ERROR) << "Unable to convert IPv4 address to string: " << address;
270 return "";
271}
272
Paul Stewart65bcd082012-11-16 09:37:14 -0800273// static
274bool DHCPConfig::ParseClasslessStaticRoutes(const string &classless_routes,
275 IPConfig::Properties *properties) {
276 if (classless_routes.empty()) {
277 // It is not an error for this string to be empty.
278 return true;
279 }
280
281 vector<string> route_strings;
282 base::SplitString(classless_routes, ' ', &route_strings);
283 if (route_strings.size() % 2) {
284 LOG(ERROR) << "In " << __func__ << ": Size of route_strings array "
285 << "is a non-even number: " << route_strings.size();
286 return false;
287 }
288
289 vector<IPConfig::Route> routes;
290 vector<string>::iterator route_iterator = route_strings.begin();
291 // Classless routes are a space-delimited array of
292 // "destination/prefix gateway" values. As such, we iterate twice
293 // for each pass of the loop below.
294 while (route_iterator != route_strings.end()) {
295 const string &destination_as_string(*route_iterator);
296 route_iterator++;
297 IPAddress destination(IPAddress::kFamilyIPv4);
298 if (!destination.SetAddressAndPrefixFromString(
299 destination_as_string)) {
300 LOG(ERROR) << "In " << __func__ << ": Expected an IP address/prefix "
301 << "but got an unparsable: " << destination_as_string;
302 return false;
303 }
304
305 CHECK(route_iterator != route_strings.end());
306 const string &gateway_as_string(*route_iterator);
307 route_iterator++;
308 IPAddress gateway(IPAddress::kFamilyIPv4);
309 if (!gateway.SetAddressFromString(gateway_as_string)) {
310 LOG(ERROR) << "In " << __func__ << ": Expected a router IP address "
311 << "but got an unparsable: " << gateway_as_string;
312 return false;
313 }
314
315 if (destination.prefix() == 0 && properties->gateway.empty()) {
316 // If a default route is provided in the classless parameters and
317 // we don't already have one, apply this as the default route.
318 SLOG(DHCP, 2) << "In " << __func__ << ": Setting default gateway to "
319 << gateway_as_string;
320 CHECK(gateway.IntoString(&properties->gateway));
321 } else {
322 IPConfig::Route route;
323 CHECK(destination.IntoString(&route.host));
324 IPAddress netmask(IPAddress::GetAddressMaskFromPrefix(
325 destination.family(), destination.prefix()));
326 CHECK(netmask.IntoString(&route.netmask));
327 CHECK(gateway.IntoString(&route.gateway));
328 routes.push_back(route);
329 SLOG(DHCP, 2) << "In " << __func__ << ": Adding route to to "
330 << destination_as_string << " via " << gateway_as_string;
331 }
332 }
333
334 if (!routes.empty()) {
335 properties->routes.swap(routes);
336 }
337
338 return true;
339}
340
341bool DHCPConfig::ParseConfiguration(const Configuration &configuration,
Darin Petkove7cb7f82011-06-03 13:21:51 -0700342 IPConfig::Properties *properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700343 SLOG(DHCP, 2) << __func__;
Chris Masone43b48a12011-07-01 13:37:07 -0700344 properties->method = flimflam::kTypeDHCP;
Paul Stewart7355ce12011-09-02 10:47:01 -0700345 properties->address_family = IPAddress::kFamilyIPv4;
Paul Stewart65bcd082012-11-16 09:37:14 -0800346 string classless_static_routes;
347 bool default_gateway_parse_error = false;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700348 for (Configuration::const_iterator it = configuration.begin();
349 it != configuration.end(); ++it) {
350 const string &key = it->first;
351 const DBus::Variant &value = it->second;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700352 SLOG(DHCP, 2) << "Processing key: " << key;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700353 if (key == kConfigurationKeyIPAddress) {
354 properties->address = GetIPv4AddressString(value.reader().get_uint32());
355 if (properties->address.empty()) {
356 return false;
357 }
358 } else if (key == kConfigurationKeySubnetCIDR) {
Paul Stewart48100b02012-03-19 07:53:52 -0700359 properties->subnet_prefix = value.reader().get_byte();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700360 } else if (key == kConfigurationKeyBroadcastAddress) {
361 properties->broadcast_address =
362 GetIPv4AddressString(value.reader().get_uint32());
363 if (properties->broadcast_address.empty()) {
364 return false;
365 }
366 } else if (key == kConfigurationKeyRouters) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700367 vector<unsigned int> routers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700368 if (routers.empty()) {
369 LOG(ERROR) << "No routers provided.";
Paul Stewart65bcd082012-11-16 09:37:14 -0800370 default_gateway_parse_error = true;
371 } else {
372 properties->gateway = GetIPv4AddressString(routers[0]);
373 if (properties->gateway.empty()) {
374 LOG(ERROR) << "Failed to parse router parameter provided.";
375 default_gateway_parse_error = true;
376 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700377 }
378 } else if (key == kConfigurationKeyDNS) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700379 vector<unsigned int> servers = value.operator vector<unsigned int>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700380 for (vector<unsigned int>::const_iterator it = servers.begin();
381 it != servers.end(); ++it) {
382 string server = GetIPv4AddressString(*it);
383 if (server.empty()) {
384 return false;
385 }
386 properties->dns_servers.push_back(server);
387 }
388 } else if (key == kConfigurationKeyDomainName) {
389 properties->domain_name = value.reader().get_string();
390 } else if (key == kConfigurationKeyDomainSearch) {
Darin Petkovf7897bc2011-06-08 17:13:36 -0700391 properties->domain_search = value.operator vector<string>();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700392 } else if (key == kConfigurationKeyMTU) {
393 int mtu = value.reader().get_uint16();
Darin Petkov14c29ec2012-03-02 11:34:19 +0100394 if (mtu >= kMinMTU) {
Darin Petkove7cb7f82011-06-03 13:21:51 -0700395 properties->mtu = mtu;
396 }
Paul Stewart65bcd082012-11-16 09:37:14 -0800397 } else if (key == kConfigurationKeyClasslessStaticRoutes) {
398 classless_static_routes = value.reader().get_string();
Darin Petkove7cb7f82011-06-03 13:21:51 -0700399 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700400 SLOG(DHCP, 2) << "Key ignored.";
Darin Petkove7cb7f82011-06-03 13:21:51 -0700401 }
402 }
Paul Stewart65bcd082012-11-16 09:37:14 -0800403 ParseClasslessStaticRoutes(classless_static_routes, properties);
404 if (default_gateway_parse_error && properties->gateway.empty()) {
405 return false;
406 }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700407 return true;
408}
409
Darin Petkov92c43902011-06-09 20:46:06 -0700410void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) {
mukesh agrawal6c6655d2012-12-06 14:49:50 -0800411 if (status == EXIT_SUCCESS) {
412 SLOG(DHCP, 2) << "pid " << pid << " exit status " << status;
413 } else {
414 LOG(WARNING) << "pid " << pid << " exit status " << status;
415 }
Darin Petkov92c43902011-06-09 20:46:06 -0700416 DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data);
417 config->child_watch_tag_ = 0;
Darin Petkov92c43902011-06-09 20:46:06 -0700418 CHECK_EQ(pid, config->pid_);
Darin Petkov92c43902011-06-09 20:46:06 -0700419 // |config| instance may be destroyed after this call.
Darin Petkov3fe17662013-02-04 14:19:08 +0100420 config->CleanupClientState();
Darin Petkov92c43902011-06-09 20:46:06 -0700421}
422
423void DHCPConfig::CleanupClientState() {
Darin Petkov3fe17662013-02-04 14:19:08 +0100424 SLOG(DHCP, 2) << __func__ << ": " << device_name();
425 StopDHCPTimeout();
Darin Petkov98dd6a02011-06-10 15:12:57 -0700426 if (child_watch_tag_) {
427 glib_->SourceRemove(child_watch_tag_);
428 child_watch_tag_ = 0;
429 }
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700430 proxy_.reset();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700431 if (lease_file_suffix_ == device_name()) {
432 // If the lease file suffix was left as default, clean it up at exit.
433 file_util::Delete(root_.Append(
434 base::StringPrintf(kDHCPCDPathFormatLease,
435 device_name().c_str())), false);
436 }
437 file_util::Delete(root_.Append(
438 base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false);
Darin Petkov3fe17662013-02-04 14:19:08 +0100439 if (pid_) {
440 int pid = pid_;
441 pid_ = 0;
442 // |this| instance may be destroyed after this call.
443 provider_->UnbindPID(pid);
444 }
Darin Petkov92c43902011-06-09 20:46:06 -0700445}
446
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700447void DHCPConfig::StartDHCPTimeout() {
448 lease_acquisition_timeout_callback_.Reset(
449 Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr()));
450 dispatcher_->PostDelayedTask(
451 lease_acquisition_timeout_callback_.callback(),
452 lease_acquisition_timeout_seconds_ * 1000);
453}
454
455void DHCPConfig::StopDHCPTimeout() {
456 lease_acquisition_timeout_callback_.Cancel();
457}
458
459void DHCPConfig::ProcessDHCPTimeout() {
460 LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " "
461 << "(after " << lease_acquisition_timeout_seconds_ << " seconds).";
462 UpdateProperties(IPConfig::Properties(), false);
463}
464
Darin Petkov50308cd2011-06-01 18:25:07 -0700465} // namespace shill