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