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> |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 8 | #include <sys/wait.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 9 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 10 | #include <base/file_util.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 13 | |
| 14 | #include "shill/dhcpcd_proxy.h" |
| 15 | #include "shill/dhcp_provider.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 16 | #include "shill/event_dispatcher.h" |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 17 | #include "shill/glib.h" |
Paul Stewart | 1d18e8c | 2011-07-15 11:00:31 -0700 | [diff] [blame] | 18 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 19 | #include "shill/logging.h" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 20 | #include "shill/minijail.h" |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 21 | #include "shill/proxy_factory.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 22 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 23 | using std::string; |
| 24 | using std::vector; |
| 25 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 26 | namespace shill { |
| 27 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 28 | // static |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 29 | const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress"; |
| 30 | const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers"; |
| 31 | const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName"; |
| 32 | const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch"; |
| 33 | const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress"; |
| 34 | const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU"; |
| 35 | const char DHCPConfig::kConfigurationKeyRouters[] = "Routers"; |
| 36 | const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR"; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 37 | const int DHCPConfig::kDHCPCDExitPollMilliseconds = 50; |
| 38 | const int DHCPConfig::kDHCPCDExitWaitMilliseconds = 3000; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 39 | const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd"; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 40 | const char DHCPConfig::kDHCPCDPathFormatLease[] = |
| 41 | "var/lib/dhcpcd/dhcpcd-%s.lease"; |
Jorge Lucangeli Obes | 2f3169d | 2012-04-25 11:38:25 -0700 | [diff] [blame] | 42 | const char DHCPConfig::kDHCPCDPathFormatPID[] = |
| 43 | "var/run/dhcpcd/dhcpcd-%s.pid"; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 44 | const int DHCPConfig::kDHCPTimeoutSeconds = 30; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 45 | const char DHCPConfig::kDHCPCDUser[] = "dhcp"; |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 46 | const int DHCPConfig::kMinMTU = 576; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 47 | const char DHCPConfig::kReasonBound[] = "BOUND"; |
| 48 | const char DHCPConfig::kReasonFail[] = "FAIL"; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 49 | const char DHCPConfig::kReasonGatewayArp[] = "GATEWAY-ARP"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 50 | const char DHCPConfig::kReasonRebind[] = "REBIND"; |
| 51 | const char DHCPConfig::kReasonReboot[] = "REBOOT"; |
| 52 | const char DHCPConfig::kReasonRenew[] = "RENEW"; |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 53 | // static |
| 54 | const char DHCPConfig::kType[] = "dhcp"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 55 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 56 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 57 | DHCPConfig::DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 58 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 59 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 60 | const string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 61 | const string &request_hostname, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 62 | const string &lease_file_suffix, |
| 63 | bool arp_gateway, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 64 | GLib *glib) |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 65 | : IPConfig(control_interface, device_name, kType), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 66 | proxy_factory_(ProxyFactory::GetInstance()), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 67 | provider_(provider), |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 68 | request_hostname_(request_hostname), |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 69 | lease_file_suffix_(lease_file_suffix), |
| 70 | arp_gateway_(arp_gateway), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 71 | pid_(0), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 72 | child_watch_tag_(0), |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 73 | lease_acquisition_timeout_seconds_(kDHCPTimeoutSeconds), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 74 | root_("/"), |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 75 | weak_ptr_factory_(this), |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 76 | dispatcher_(dispatcher), |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 77 | glib_(glib), |
| 78 | minijail_(Minijail::GetInstance()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 79 | SLOG(DHCP, 2) << __func__ << ": " << device_name; |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 80 | if (lease_file_suffix_.empty()) { |
| 81 | lease_file_suffix_ = device_name; |
| 82 | } |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | DHCPConfig::~DHCPConfig() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 86 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 87 | |
| 88 | // Don't leave behind dhcpcd running. |
| 89 | Stop(); |
| 90 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 91 | // Make sure we don't get any callbacks to the destroyed instance. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 92 | CleanupClientState(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 95 | bool DHCPConfig::RequestIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 96 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 97 | if (!pid_) { |
| 98 | return Start(); |
| 99 | } |
| 100 | if (!proxy_.get()) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 101 | LOG(ERROR) << "Unable to request IP before acquiring destination."; |
| 102 | return Restart(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 103 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 104 | return RenewIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 107 | bool DHCPConfig::RenewIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 108 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 109 | if (!pid_) { |
| 110 | return false; |
| 111 | } |
Paul Stewart | c02344a | 2012-08-17 16:57:37 -0700 | [diff] [blame] | 112 | if (!proxy_.get()) { |
| 113 | LOG(ERROR) << "Unable to renew IP before acquiring destination."; |
| 114 | return false; |
| 115 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 116 | proxy_->Rebind(device_name()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 117 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 121 | bool DHCPConfig::ReleaseIP() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 122 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 123 | if (!pid_) { |
| 124 | return true; |
| 125 | } |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 126 | // If we are using gateway unicast ARP to speed up re-connect, don't |
| 127 | // give up our leases when we disconnect. |
| 128 | if (!arp_gateway_ && proxy_.get()) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 129 | proxy_->Release(device_name()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 130 | } |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 131 | Stop(); |
| 132 | return true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 135 | void DHCPConfig::InitProxy(const string &service) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 136 | if (!proxy_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 137 | SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 138 | proxy_.reset(proxy_factory_->CreateDHCPProxy(service)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 142 | void DHCPConfig::ProcessEventSignal(const string &reason, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 143 | const Configuration &configuration) { |
| 144 | LOG(INFO) << "Event reason: " << reason; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 145 | if (reason == kReasonFail) { |
| 146 | LOG(ERROR) << "Received failure event from DHCP client."; |
| 147 | UpdateProperties(IPConfig::Properties(), false); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 148 | return; |
| 149 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 150 | if (reason != kReasonBound && |
| 151 | reason != kReasonRebind && |
| 152 | reason != kReasonReboot && |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 153 | reason != kReasonRenew && |
| 154 | reason != kReasonGatewayArp) { |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 155 | LOG(WARNING) << "Event ignored."; |
| 156 | return; |
| 157 | } |
| 158 | IPConfig::Properties properties; |
| 159 | CHECK(ParseConfiguration(configuration, &properties)); |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 160 | if (reason == kReasonGatewayArp) { |
| 161 | // This is a non-authoritative confirmation that we or on the same |
| 162 | // network as the one we received a lease on previously. The DHCP |
| 163 | // client is still running, so we should not cancel the timeout |
| 164 | // until that completes. In the meantime, however, we can tentatively |
| 165 | // configure our network in anticipation of successful completion. |
| 166 | IPConfig::UpdateProperties(properties, true); |
| 167 | } else { |
| 168 | UpdateProperties(properties, true); |
| 169 | } |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 170 | } |
| 171 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 172 | void DHCPConfig::UpdateProperties(const Properties &properties, bool success) { |
| 173 | StopDHCPTimeout(); |
| 174 | IPConfig::UpdateProperties(properties, success); |
| 175 | } |
| 176 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 177 | bool DHCPConfig::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 178 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 179 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 180 | vector<char *> args; |
| 181 | args.push_back(const_cast<char *>(kDHCPCDPath)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 182 | args.push_back(const_cast<char *>("-B")); // Run in foreground. |
mukesh agrawal | 7eb0289 | 2012-05-29 11:22:37 -0700 | [diff] [blame] | 183 | args.push_back(const_cast<char *>("-q")); // Only warnings+errors to stderr. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 184 | if (!request_hostname_.empty()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 185 | args.push_back(const_cast<char *>("-h")); // Request hostname from server. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 186 | args.push_back(const_cast<char *>(request_hostname_.c_str())); |
| 187 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 188 | if (arp_gateway_) { |
| 189 | args.push_back(const_cast<char *>("-R")); // ARP for default gateway. |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 190 | args.push_back(const_cast<char *>("-U")); // Enable unicast ARP on renew. |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 191 | } |
| 192 | string interface_arg(device_name()); |
| 193 | if (lease_file_suffix_ != device_name()) { |
| 194 | interface_arg = base::StringPrintf("%s=%s", device_name().c_str(), |
| 195 | lease_file_suffix_.c_str()); |
| 196 | } |
| 197 | args.push_back(const_cast<char *>(interface_arg.c_str())); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 198 | args.push_back(NULL); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 199 | |
| 200 | struct minijail *jail = minijail_->New(); |
| 201 | minijail_->DropRoot(jail, kDHCPCDUser); |
| 202 | minijail_->UseCapabilities(jail, |
| 203 | CAP_TO_MASK(CAP_NET_BIND_SERVICE) | |
| 204 | CAP_TO_MASK(CAP_NET_BROADCAST) | |
| 205 | CAP_TO_MASK(CAP_NET_ADMIN) | |
| 206 | CAP_TO_MASK(CAP_NET_RAW)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 207 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 208 | CHECK(!pid_); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 209 | if (!minijail_->RunAndDestroy(jail, args, &pid_)) { |
| 210 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail."; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 211 | return false; |
| 212 | } |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 213 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 214 | provider_->BindPID(pid_, this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 215 | CHECK(!child_watch_tag_); |
| 216 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 217 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 218 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 221 | void DHCPConfig::Stop() { |
| 222 | if (pid_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 223 | SLOG(DHCP, 2) << "Terminating " << pid_; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 224 | if (kill(pid_, SIGTERM) < 0) { |
| 225 | PLOG(ERROR); |
| 226 | return; |
| 227 | } |
| 228 | pid_t ret; |
| 229 | int num_iterations = |
| 230 | kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds; |
| 231 | for (int count = 0; count < num_iterations; ++count) { |
| 232 | ret = waitpid(pid_, NULL, WNOHANG); |
| 233 | if (ret == pid_ || ret == -1) |
| 234 | break; |
| 235 | usleep(kDHCPCDExitPollMilliseconds * 1000); |
| 236 | if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd. |
| 237 | kill(pid_, SIGKILL); |
| 238 | } |
| 239 | if (ret != pid_) |
| 240 | PLOG(ERROR); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 241 | } |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 242 | StopDHCPTimeout(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 245 | bool DHCPConfig::Restart() { |
| 246 | // Check to ensure that this instance doesn't get destroyed in the middle of |
| 247 | // this call. If stopping a running client while there's only one reference to |
| 248 | // this instance, we will end up destroying it when the PID is unbound from |
| 249 | // the Provider. Since the Provider doesn't invoke Restart, this would mean |
| 250 | // that Restart was erroneously executed through a bare reference. |
| 251 | CHECK(!pid_ || !HasOneRef()); |
| 252 | Stop(); |
| 253 | if (pid_) { |
| 254 | provider_->UnbindPID(pid_); |
| 255 | } |
| 256 | CleanupClientState(); |
| 257 | return Start(); |
| 258 | } |
| 259 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 260 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 261 | char str[INET_ADDRSTRLEN]; |
| 262 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 263 | return str; |
| 264 | } |
| 265 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 266 | return ""; |
| 267 | } |
| 268 | |
| 269 | bool DHCPConfig::ParseConfiguration(const Configuration& configuration, |
| 270 | IPConfig::Properties *properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 271 | SLOG(DHCP, 2) << __func__; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 272 | properties->method = flimflam::kTypeDHCP; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 273 | properties->address_family = IPAddress::kFamilyIPv4; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 274 | for (Configuration::const_iterator it = configuration.begin(); |
| 275 | it != configuration.end(); ++it) { |
| 276 | const string &key = it->first; |
| 277 | const DBus::Variant &value = it->second; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 278 | SLOG(DHCP, 2) << "Processing key: " << key; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 279 | if (key == kConfigurationKeyIPAddress) { |
| 280 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 281 | if (properties->address.empty()) { |
| 282 | return false; |
| 283 | } |
| 284 | } else if (key == kConfigurationKeySubnetCIDR) { |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 285 | properties->subnet_prefix = value.reader().get_byte(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 286 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 287 | properties->broadcast_address = |
| 288 | GetIPv4AddressString(value.reader().get_uint32()); |
| 289 | if (properties->broadcast_address.empty()) { |
| 290 | return false; |
| 291 | } |
| 292 | } else if (key == kConfigurationKeyRouters) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 293 | vector<unsigned int> routers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 294 | if (routers.empty()) { |
| 295 | LOG(ERROR) << "No routers provided."; |
| 296 | return false; |
| 297 | } |
| 298 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 299 | if (properties->gateway.empty()) { |
| 300 | return false; |
| 301 | } |
| 302 | } else if (key == kConfigurationKeyDNS) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 303 | vector<unsigned int> servers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 304 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 305 | it != servers.end(); ++it) { |
| 306 | string server = GetIPv4AddressString(*it); |
| 307 | if (server.empty()) { |
| 308 | return false; |
| 309 | } |
| 310 | properties->dns_servers.push_back(server); |
| 311 | } |
| 312 | } else if (key == kConfigurationKeyDomainName) { |
| 313 | properties->domain_name = value.reader().get_string(); |
| 314 | } else if (key == kConfigurationKeyDomainSearch) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 315 | properties->domain_search = value.operator vector<string>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 316 | } else if (key == kConfigurationKeyMTU) { |
| 317 | int mtu = value.reader().get_uint16(); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 318 | if (mtu >= kMinMTU) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 319 | properties->mtu = mtu; |
| 320 | } |
| 321 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 322 | SLOG(DHCP, 2) << "Key ignored."; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | return true; |
| 326 | } |
| 327 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 328 | void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 329 | SLOG(DHCP, 2) << "pid " << pid << " exit status " << status; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 330 | DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data); |
| 331 | config->child_watch_tag_ = 0; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 332 | CHECK_EQ(pid, config->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 333 | config->CleanupClientState(); |
| 334 | |
| 335 | // |config| instance may be destroyed after this call. |
| 336 | config->provider_->UnbindPID(pid); |
| 337 | } |
| 338 | |
| 339 | void DHCPConfig::CleanupClientState() { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 340 | if (child_watch_tag_) { |
| 341 | glib_->SourceRemove(child_watch_tag_); |
| 342 | child_watch_tag_ = 0; |
| 343 | } |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 344 | pid_ = 0; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 345 | proxy_.reset(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 346 | if (lease_file_suffix_ == device_name()) { |
| 347 | // If the lease file suffix was left as default, clean it up at exit. |
| 348 | file_util::Delete(root_.Append( |
| 349 | base::StringPrintf(kDHCPCDPathFormatLease, |
| 350 | device_name().c_str())), false); |
| 351 | } |
| 352 | file_util::Delete(root_.Append( |
| 353 | base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 354 | } |
| 355 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 356 | void DHCPConfig::StartDHCPTimeout() { |
| 357 | lease_acquisition_timeout_callback_.Reset( |
| 358 | Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 359 | dispatcher_->PostDelayedTask( |
| 360 | lease_acquisition_timeout_callback_.callback(), |
| 361 | lease_acquisition_timeout_seconds_ * 1000); |
| 362 | } |
| 363 | |
| 364 | void DHCPConfig::StopDHCPTimeout() { |
| 365 | lease_acquisition_timeout_callback_.Cancel(); |
| 366 | } |
| 367 | |
| 368 | void DHCPConfig::ProcessDHCPTimeout() { |
| 369 | LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " " |
| 370 | << "(after " << lease_acquisition_timeout_seconds_ << " seconds)."; |
| 371 | UpdateProperties(IPConfig::Properties(), false); |
| 372 | } |
| 373 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 374 | } // namespace shill |