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