Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 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 Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 7 | #include <arpa/inet.h> |
mukesh agrawal | 6c6655d | 2012-12-06 14:49:50 -0800 | [diff] [blame] | 8 | #include <stdlib.h> |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 9 | #include <sys/wait.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 10 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 11 | #include <base/file_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 12 | #include <base/strings/string_split.h> |
| 13 | #include <base/strings/stringprintf.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 15 | |
| 16 | #include "shill/dhcpcd_proxy.h" |
| 17 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 18 | #include "shill/event_dispatcher.h" |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 19 | #include "shill/glib.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 20 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 21 | #include "shill/logging.h" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 22 | #include "shill/minijail.h" |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 23 | #include "shill/proxy_factory.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 24 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | using std::vector; |
| 27 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 28 | namespace shill { |
| 29 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 30 | // static |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 31 | const int DHCPConfig::kAcquisitionTimeoutSeconds = 30; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 32 | const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress"; |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 33 | const char DHCPConfig::kConfigurationKeyClasslessStaticRoutes[] = |
| 34 | "ClasslessStaticRoutes"; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 35 | const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers"; |
| 36 | const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName"; |
| 37 | const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch"; |
| 38 | const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress"; |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 39 | const char DHCPConfig::kConfigurationKeyLeaseTime[] = "DHCPLeaseTime"; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU"; |
| 41 | const char DHCPConfig::kConfigurationKeyRouters[] = "Routers"; |
| 42 | const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR"; |
Paul Stewart | c3fdba9 | 2013-12-02 11:12:38 -0800 | [diff] [blame] | 43 | const char DHCPConfig::kConfigurationKeyVendorEncapsulatedOptions[] = |
| 44 | "VendorEncapsulatedOptions"; |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 45 | const char DHCPConfig::kConfigurationKeyWebProxyAutoDiscoveryUrl[] = |
| 46 | "WebProxyAutoDiscoveryUrl"; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 47 | const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50; |
| 48 | const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000; |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 49 | const char DHCPConfig::kDHCPCDMinimalConfig[] = "/etc/dhcpcd-minimal.conf"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 50 | const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd"; |
Jorge Lucangeli Obes | 2f3169d | 2012-04-25 11:38:25 -0700 | [diff] [blame] | 51 | const char DHCPConfig::kDHCPCDPathFormatPID[] = |
| 52 | "var/run/dhcpcd/dhcpcd-%s.pid"; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 53 | const char DHCPConfig::kDHCPCDUser[] = "dhcp"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 54 | const int DHCPConfig::kMinMTU = 576; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 55 | const char DHCPConfig::kReasonBound[] = "BOUND"; |
| 56 | const char DHCPConfig::kReasonFail[] = "FAIL"; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 57 | const char DHCPConfig::kReasonGatewayArp[] = "GATEWAY-ARP"; |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 58 | const char DHCPConfig::kReasonNak[] = "NAK"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 59 | const char DHCPConfig::kReasonRebind[] = "REBIND"; |
| 60 | const char DHCPConfig::kReasonReboot[] = "REBOOT"; |
| 61 | const char DHCPConfig::kReasonRenew[] = "RENEW"; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 62 | // static |
| 63 | const char DHCPConfig::kType[] = "dhcp"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 64 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 65 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 66 | DHCPConfig::DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 67 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 68 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 69 | const string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 70 | const string &request_hostname, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 71 | const string &lease_file_suffix, |
| 72 | bool arp_gateway, |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 73 | bool is_minimal_config, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 74 | GLib *glib) |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 75 | : IPConfig(control_interface, device_name, kType), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 76 | proxy_factory_(ProxyFactory::GetInstance()), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 77 | provider_(provider), |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 78 | request_hostname_(request_hostname), |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 79 | lease_file_suffix_(lease_file_suffix), |
| 80 | arp_gateway_(arp_gateway), |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 81 | is_minimal_config_(is_minimal_config), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 82 | pid_(0), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 83 | child_watch_tag_(0), |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 84 | is_lease_active_(false), |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 85 | is_gateway_arp_active_(false), |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 86 | lease_acquisition_timeout_seconds_(kAcquisitionTimeoutSeconds), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 87 | root_("/"), |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 88 | weak_ptr_factory_(this), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 89 | dispatcher_(dispatcher), |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 90 | glib_(glib), |
| 91 | minijail_(Minijail::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 92 | SLOG(DHCP, 2) << __func__ << ": " << device_name; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 93 | if (lease_file_suffix_.empty()) { |
| 94 | lease_file_suffix_ = device_name; |
| 95 | } |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | DHCPConfig::~DHCPConfig() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 99 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 100 | |
| 101 | // Don't leave behind dhcpcd running. |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 102 | Stop(__func__); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 105 | bool DHCPConfig::RequestIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 106 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 107 | if (!pid_) { |
| 108 | return Start(); |
| 109 | } |
| 110 | if (!proxy_.get()) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 111 | LOG(ERROR) << "Unable to request IP before acquiring destination."; |
| 112 | return Restart(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 113 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 114 | return RenewIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 117 | bool DHCPConfig::RenewIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 118 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 119 | if (!pid_) { |
Paul Stewart | 21f7b34 | 2013-11-19 10:15:58 -0800 | [diff] [blame] | 120 | return Start(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 121 | } |
Paul Stewart | c02344a | 2012-08-17 16:57:37 -0700 | [diff] [blame] | 122 | if (!proxy_.get()) { |
| 123 | LOG(ERROR) << "Unable to renew IP before acquiring destination."; |
| 124 | return false; |
| 125 | } |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 126 | StopExpirationTimeout(); |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 127 | proxy_->Rebind(device_name()); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 128 | StartAcquisitionTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 132 | bool DHCPConfig::ReleaseIP(ReleaseReason reason) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 133 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 134 | if (!pid_) { |
| 135 | return true; |
| 136 | } |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 137 | |
| 138 | // If we are using static IP and haven't retrieved a lease yet, we should |
| 139 | // allow the DHCP process to continue until we have a lease. |
| 140 | if (!is_lease_active_ && reason == IPConfig::kReleaseReasonStaticIP) { |
| 141 | return true; |
| 142 | } |
| 143 | |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 144 | // If we are using gateway unicast ARP to speed up re-connect, don't |
| 145 | // give up our leases when we disconnect. |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 146 | bool should_keep_lease = |
| 147 | reason == IPConfig::kReleaseReasonDisconnect && arp_gateway_; |
| 148 | |
| 149 | if (!should_keep_lease && proxy_.get()) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 150 | proxy_->Release(device_name()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 151 | } |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 152 | Stop(__func__); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 153 | return true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 156 | void DHCPConfig::InitProxy(const string &service) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 157 | if (!proxy_.get()) { |
Christopher Wiley | 0236e76 | 2012-11-16 14:11:05 -0800 | [diff] [blame] | 158 | LOG(INFO) << "Init DHCP Proxy: " << device_name() << " at " << service; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 159 | proxy_.reset(proxy_factory_->CreateDHCPProxy(service)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 163 | void DHCPConfig::ProcessEventSignal(const string &reason, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 164 | const Configuration &configuration) { |
| 165 | LOG(INFO) << "Event reason: " << reason; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 166 | if (reason == kReasonFail) { |
| 167 | LOG(ERROR) << "Received failure event from DHCP client."; |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 168 | NotifyFailure(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 169 | return; |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 170 | } else if (reason == kReasonNak) { |
| 171 | // If we got a NAK, this means the DHCP server is active, and any |
| 172 | // Gateway ARP state we have is no longer sufficient. |
| 173 | LOG_IF(ERROR, is_gateway_arp_active_) |
| 174 | << "Received NAK event for our gateway-ARP lease."; |
| 175 | is_gateway_arp_active_ = false; |
| 176 | return; |
| 177 | } else if (reason != kReasonBound && |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 178 | reason != kReasonRebind && |
| 179 | reason != kReasonReboot && |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 180 | reason != kReasonRenew && |
| 181 | reason != kReasonGatewayArp) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 182 | LOG(WARNING) << "Event ignored."; |
| 183 | return; |
| 184 | } |
| 185 | IPConfig::Properties properties; |
| 186 | CHECK(ParseConfiguration(configuration, &properties)); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 187 | |
| 188 | // This needs to be set before calling UpdateProperties() below since |
| 189 | // those functions may indirectly call other methods like ReleaseIP that |
| 190 | // depend on or change this value. |
| 191 | is_lease_active_ = true; |
| 192 | |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 193 | if (reason == kReasonGatewayArp) { |
| 194 | // This is a non-authoritative confirmation that we or on the same |
| 195 | // network as the one we received a lease on previously. The DHCP |
| 196 | // client is still running, so we should not cancel the timeout |
| 197 | // until that completes. In the meantime, however, we can tentatively |
| 198 | // configure our network in anticipation of successful completion. |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 199 | IPConfig::UpdateProperties(properties); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 200 | is_gateway_arp_active_ = true; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 201 | } else { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 202 | UpdateProperties(properties); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 203 | is_gateway_arp_active_ = false; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 204 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 207 | void DHCPConfig::UpdateProperties(const Properties &properties) { |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 208 | StopAcquisitionTimeout(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 209 | IPConfig::UpdateProperties(properties); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 210 | if (properties.lease_duration_seconds) { |
| 211 | StartExpirationTimeout(properties.lease_duration_seconds); |
| 212 | } else { |
| 213 | LOG(WARNING) << "Lease duration is zero; not starting an expiration timer."; |
| 214 | StopExpirationTimeout(); |
| 215 | } |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void DHCPConfig::NotifyFailure() { |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 219 | StopAcquisitionTimeout(); |
| 220 | StopExpirationTimeout(); |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 221 | IPConfig::NotifyFailure(); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 224 | bool DHCPConfig::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 225 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 226 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 227 | // TODO(quiche): This should be migrated to use ExternalTask. |
| 228 | // (crbug.com/246263). |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 229 | vector<char *> args; |
| 230 | args.push_back(const_cast<char *>(kDHCPCDPath)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 231 | args.push_back(const_cast<char *>("-B")); // Run in foreground. |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 232 | args.push_back(const_cast<char *>("-q")); // Only warnings+errors to stderr. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 233 | if (!request_hostname_.empty()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 234 | args.push_back(const_cast<char *>("-h")); // Request hostname from server. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 235 | args.push_back(const_cast<char *>(request_hostname_.c_str())); |
| 236 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 237 | if (arp_gateway_) { |
| 238 | args.push_back(const_cast<char *>("-R")); // ARP for default gateway. |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 239 | args.push_back(const_cast<char *>("-U")); // Enable unicast ARP on renew. |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 240 | } |
Paul Stewart | 75a68b9 | 2013-10-24 10:50:27 -0700 | [diff] [blame] | 241 | if (is_minimal_config_) { |
| 242 | args.push_back(const_cast<char *>("-f")); // Supply a configuration file. |
| 243 | args.push_back(const_cast<char *>(kDHCPCDMinimalConfig)); |
| 244 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 245 | string interface_arg(device_name()); |
| 246 | if (lease_file_suffix_ != device_name()) { |
| 247 | interface_arg = base::StringPrintf("%s=%s", device_name().c_str(), |
| 248 | lease_file_suffix_.c_str()); |
| 249 | } |
| 250 | args.push_back(const_cast<char *>(interface_arg.c_str())); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 251 | args.push_back(NULL); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 252 | |
| 253 | struct minijail *jail = minijail_->New(); |
| 254 | minijail_->DropRoot(jail, kDHCPCDUser); |
| 255 | minijail_->UseCapabilities(jail, |
| 256 | CAP_TO_MASK(CAP_NET_BIND_SERVICE) | |
| 257 | CAP_TO_MASK(CAP_NET_BROADCAST) | |
| 258 | CAP_TO_MASK(CAP_NET_ADMIN) | |
| 259 | CAP_TO_MASK(CAP_NET_RAW)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 260 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 261 | CHECK(!pid_); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 262 | if (!minijail_->RunAndDestroy(jail, args, &pid_)) { |
| 263 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail."; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 266 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 267 | provider_->BindPID(pid_, this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 268 | CHECK(!child_watch_tag_); |
| 269 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 270 | StartAcquisitionTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 271 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 272 | } |
| 273 | |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 274 | void DHCPConfig::Stop(const char *reason) { |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 275 | LOG_IF(INFO, pid_) << "Stopping " << pid_ << " (" << reason << ")"; |
| 276 | KillClient(); |
| 277 | // KillClient waits for the client to terminate so it's safe to cleanup the |
| 278 | // state. |
| 279 | CleanupClientState(); |
| 280 | } |
| 281 | |
| 282 | void DHCPConfig::KillClient() { |
| 283 | if (!pid_) { |
| 284 | return; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 285 | } |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 286 | if (kill(pid_, SIGTERM) < 0) { |
| 287 | PLOG(ERROR); |
| 288 | return; |
| 289 | } |
| 290 | pid_t ret; |
| 291 | int num_iterations = |
| 292 | kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds; |
| 293 | for (int count = 0; count < num_iterations; ++count) { |
| 294 | ret = waitpid(pid_, NULL, WNOHANG); |
| 295 | if (ret == pid_ || ret == -1) |
| 296 | break; |
| 297 | usleep(kDHCPCDExitPollMilliseconds * 1000); |
| 298 | if (count == num_iterations / 2) { |
| 299 | // Make one last attempt to kill dhcpcd. |
| 300 | LOG(WARNING) << "Terminating " << pid_ << " with SIGKILL."; |
| 301 | kill(pid_, SIGKILL); |
| 302 | } |
| 303 | } |
| 304 | if (ret != pid_) |
| 305 | PLOG(ERROR); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 308 | bool DHCPConfig::Restart() { |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 309 | // Take a reference of this instance to make sure we don't get destroyed in |
| 310 | // the middle of this call. |
| 311 | DHCPConfigRefPtr me = this; |
| 312 | me->Stop(__func__); |
| 313 | return me->Start(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 314 | } |
| 315 | |
mukesh agrawal | 28fd88e | 2013-05-02 14:06:10 -0700 | [diff] [blame] | 316 | // static |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 317 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 318 | char str[INET_ADDRSTRLEN]; |
| 319 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 320 | return str; |
| 321 | } |
| 322 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 323 | return ""; |
| 324 | } |
| 325 | |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 326 | // static |
| 327 | bool DHCPConfig::ParseClasslessStaticRoutes(const string &classless_routes, |
| 328 | IPConfig::Properties *properties) { |
| 329 | if (classless_routes.empty()) { |
| 330 | // It is not an error for this string to be empty. |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | vector<string> route_strings; |
| 335 | base::SplitString(classless_routes, ' ', &route_strings); |
| 336 | if (route_strings.size() % 2) { |
| 337 | LOG(ERROR) << "In " << __func__ << ": Size of route_strings array " |
| 338 | << "is a non-even number: " << route_strings.size(); |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | vector<IPConfig::Route> routes; |
| 343 | vector<string>::iterator route_iterator = route_strings.begin(); |
| 344 | // Classless routes are a space-delimited array of |
| 345 | // "destination/prefix gateway" values. As such, we iterate twice |
| 346 | // for each pass of the loop below. |
| 347 | while (route_iterator != route_strings.end()) { |
| 348 | const string &destination_as_string(*route_iterator); |
| 349 | route_iterator++; |
| 350 | IPAddress destination(IPAddress::kFamilyIPv4); |
| 351 | if (!destination.SetAddressAndPrefixFromString( |
| 352 | destination_as_string)) { |
| 353 | LOG(ERROR) << "In " << __func__ << ": Expected an IP address/prefix " |
| 354 | << "but got an unparsable: " << destination_as_string; |
| 355 | return false; |
| 356 | } |
| 357 | |
| 358 | CHECK(route_iterator != route_strings.end()); |
| 359 | const string &gateway_as_string(*route_iterator); |
| 360 | route_iterator++; |
| 361 | IPAddress gateway(IPAddress::kFamilyIPv4); |
| 362 | if (!gateway.SetAddressFromString(gateway_as_string)) { |
| 363 | LOG(ERROR) << "In " << __func__ << ": Expected a router IP address " |
| 364 | << "but got an unparsable: " << gateway_as_string; |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | if (destination.prefix() == 0 && properties->gateway.empty()) { |
| 369 | // If a default route is provided in the classless parameters and |
| 370 | // we don't already have one, apply this as the default route. |
| 371 | SLOG(DHCP, 2) << "In " << __func__ << ": Setting default gateway to " |
| 372 | << gateway_as_string; |
| 373 | CHECK(gateway.IntoString(&properties->gateway)); |
| 374 | } else { |
| 375 | IPConfig::Route route; |
| 376 | CHECK(destination.IntoString(&route.host)); |
| 377 | IPAddress netmask(IPAddress::GetAddressMaskFromPrefix( |
| 378 | destination.family(), destination.prefix())); |
| 379 | CHECK(netmask.IntoString(&route.netmask)); |
| 380 | CHECK(gateway.IntoString(&route.gateway)); |
| 381 | routes.push_back(route); |
| 382 | SLOG(DHCP, 2) << "In " << __func__ << ": Adding route to to " |
| 383 | << destination_as_string << " via " << gateway_as_string; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | if (!routes.empty()) { |
| 388 | properties->routes.swap(routes); |
| 389 | } |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
mukesh agrawal | 28fd88e | 2013-05-02 14:06:10 -0700 | [diff] [blame] | 394 | // static |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 395 | bool DHCPConfig::ParseConfiguration(const Configuration &configuration, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 396 | IPConfig::Properties *properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 397 | SLOG(DHCP, 2) << __func__; |
Ben Chan | 0295e0f | 2013-09-20 13:47:29 -0700 | [diff] [blame] | 398 | properties->method = kTypeDHCP; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 399 | properties->address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 400 | string classless_static_routes; |
| 401 | bool default_gateway_parse_error = false; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 402 | for (Configuration::const_iterator it = configuration.begin(); |
| 403 | it != configuration.end(); ++it) { |
| 404 | const string &key = it->first; |
| 405 | const DBus::Variant &value = it->second; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 406 | SLOG(DHCP, 2) << "Processing key: " << key; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 407 | if (key == kConfigurationKeyIPAddress) { |
| 408 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 409 | if (properties->address.empty()) { |
| 410 | return false; |
| 411 | } |
| 412 | } else if (key == kConfigurationKeySubnetCIDR) { |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 413 | properties->subnet_prefix = value.reader().get_byte(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 414 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 415 | properties->broadcast_address = |
| 416 | GetIPv4AddressString(value.reader().get_uint32()); |
| 417 | if (properties->broadcast_address.empty()) { |
| 418 | return false; |
| 419 | } |
| 420 | } else if (key == kConfigurationKeyRouters) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 421 | vector<unsigned int> routers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 422 | if (routers.empty()) { |
| 423 | LOG(ERROR) << "No routers provided."; |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 424 | default_gateway_parse_error = true; |
| 425 | } else { |
| 426 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 427 | if (properties->gateway.empty()) { |
| 428 | LOG(ERROR) << "Failed to parse router parameter provided."; |
| 429 | default_gateway_parse_error = true; |
| 430 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 431 | } |
| 432 | } else if (key == kConfigurationKeyDNS) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 433 | vector<unsigned int> servers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 434 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 435 | it != servers.end(); ++it) { |
| 436 | string server = GetIPv4AddressString(*it); |
| 437 | if (server.empty()) { |
| 438 | return false; |
| 439 | } |
| 440 | properties->dns_servers.push_back(server); |
| 441 | } |
| 442 | } else if (key == kConfigurationKeyDomainName) { |
| 443 | properties->domain_name = value.reader().get_string(); |
| 444 | } else if (key == kConfigurationKeyDomainSearch) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 445 | properties->domain_search = value.operator vector<string>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 446 | } else if (key == kConfigurationKeyMTU) { |
| 447 | int mtu = value.reader().get_uint16(); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 448 | if (mtu >= kMinMTU) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 449 | properties->mtu = mtu; |
| 450 | } |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 451 | } else if (key == kConfigurationKeyClasslessStaticRoutes) { |
| 452 | classless_static_routes = value.reader().get_string(); |
Paul Stewart | c3fdba9 | 2013-12-02 11:12:38 -0800 | [diff] [blame] | 453 | } else if (key == kConfigurationKeyVendorEncapsulatedOptions) { |
| 454 | properties->vendor_encapsulated_options = value.reader().get_string(); |
Paul Stewart | a63f521 | 2013-06-25 15:29:40 -0700 | [diff] [blame] | 455 | } else if (key == kConfigurationKeyWebProxyAutoDiscoveryUrl) { |
| 456 | properties->web_proxy_auto_discovery = value.reader().get_string(); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 457 | } else if (key == kConfigurationKeyLeaseTime) { |
| 458 | properties->lease_duration_seconds = value.reader().get_uint32(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 459 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 460 | SLOG(DHCP, 2) << "Key ignored."; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 463 | ParseClasslessStaticRoutes(classless_static_routes, properties); |
| 464 | if (default_gateway_parse_error && properties->gateway.empty()) { |
| 465 | return false; |
| 466 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 470 | void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) { |
mukesh agrawal | 6c6655d | 2012-12-06 14:49:50 -0800 | [diff] [blame] | 471 | if (status == EXIT_SUCCESS) { |
| 472 | SLOG(DHCP, 2) << "pid " << pid << " exit status " << status; |
| 473 | } else { |
| 474 | LOG(WARNING) << "pid " << pid << " exit status " << status; |
| 475 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 476 | DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data); |
| 477 | config->child_watch_tag_ = 0; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 478 | CHECK_EQ(pid, config->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 479 | // |config| instance may be destroyed after this call. |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 480 | config->CleanupClientState(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void DHCPConfig::CleanupClientState() { |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 484 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 485 | StopAcquisitionTimeout(); |
| 486 | StopExpirationTimeout(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 487 | if (child_watch_tag_) { |
| 488 | glib_->SourceRemove(child_watch_tag_); |
| 489 | child_watch_tag_ = 0; |
| 490 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 491 | proxy_.reset(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 492 | if (lease_file_suffix_ == device_name()) { |
| 493 | // If the lease file suffix was left as default, clean it up at exit. |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 494 | base::DeleteFile(root_.Append( |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 495 | base::StringPrintf(DHCPProvider::kDHCPCDPathFormatLease, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 496 | device_name().c_str())), false); |
| 497 | } |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 498 | base::DeleteFile(root_.Append( |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 499 | base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false); |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 500 | if (pid_) { |
| 501 | int pid = pid_; |
| 502 | pid_ = 0; |
| 503 | // |this| instance may be destroyed after this call. |
| 504 | provider_->UnbindPID(pid); |
| 505 | } |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 506 | is_lease_active_ = false; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 509 | void DHCPConfig::StartAcquisitionTimeout() { |
| 510 | CHECK(lease_expiration_callback_.IsCancelled()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 511 | lease_acquisition_timeout_callback_.Reset( |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 512 | Bind(&DHCPConfig::ProcessAcquisitionTimeout, |
| 513 | weak_ptr_factory_.GetWeakPtr())); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 514 | dispatcher_->PostDelayedTask( |
| 515 | lease_acquisition_timeout_callback_.callback(), |
| 516 | lease_acquisition_timeout_seconds_ * 1000); |
| 517 | } |
| 518 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 519 | void DHCPConfig::StopAcquisitionTimeout() { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 520 | lease_acquisition_timeout_callback_.Cancel(); |
| 521 | } |
| 522 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 523 | void DHCPConfig::ProcessAcquisitionTimeout() { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 524 | LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " " |
| 525 | << "(after " << lease_acquisition_timeout_seconds_ << " seconds)."; |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 526 | if (is_gateway_arp_active_) { |
| 527 | LOG(INFO) << "Continuing to use our previous lease, due to gateway-ARP."; |
| 528 | } else { |
Paul Stewart | c509953 | 2013-12-12 07:53:15 -0800 | [diff] [blame] | 529 | NotifyFailure(); |
Paul Stewart | 94571f1 | 2013-09-10 17:26:07 -0700 | [diff] [blame] | 530 | } |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Paul Stewart | 1f916e4 | 2013-12-23 09:52:54 -0800 | [diff] [blame] | 533 | void DHCPConfig::StartExpirationTimeout(uint32 lease_duration_seconds) { |
| 534 | CHECK(lease_acquisition_timeout_callback_.IsCancelled()); |
| 535 | SLOG(DHCP, 2) << __func__ << ": " << device_name() |
| 536 | << ": " << "Lease timeout is " << lease_duration_seconds |
| 537 | << " seconds."; |
| 538 | lease_expiration_callback_.Reset( |
| 539 | Bind(&DHCPConfig::ProcessExpirationTimeout, |
| 540 | weak_ptr_factory_.GetWeakPtr())); |
| 541 | dispatcher_->PostDelayedTask( |
| 542 | lease_expiration_callback_.callback(), |
| 543 | lease_duration_seconds * 1000); |
| 544 | } |
| 545 | |
| 546 | void DHCPConfig::StopExpirationTimeout() { |
| 547 | lease_expiration_callback_.Cancel(); |
| 548 | } |
| 549 | |
| 550 | void DHCPConfig::ProcessExpirationTimeout() { |
| 551 | LOG(ERROR) << "DHCP lease expired on " << device_name() |
| 552 | << "; restarting DHCP client instance."; |
| 553 | NotifyExpiry(); |
| 554 | if (!Restart()) { |
| 555 | NotifyFailure(); |
| 556 | } |
| 557 | } |
| 558 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 559 | } // namespace shill |