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"; |
| 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 | } |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 112 | proxy_->Rebind(device_name()); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 113 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 117 | bool DHCPConfig::ReleaseIP() { |
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_) { |
| 120 | return true; |
| 121 | } |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 122 | if (proxy_.get()) { |
| 123 | proxy_->Release(device_name()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 124 | } |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 125 | Stop(); |
| 126 | return true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 129 | void DHCPConfig::InitProxy(const string &service) { |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 130 | if (!proxy_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 131 | SLOG(DHCP, 2) << "Init DHCP Proxy: " << device_name() << " at " << service; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 132 | proxy_.reset(proxy_factory_->CreateDHCPProxy(service)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 136 | void DHCPConfig::ProcessEventSignal(const string &reason, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 137 | const Configuration &configuration) { |
| 138 | LOG(INFO) << "Event reason: " << reason; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 139 | if (reason == kReasonFail) { |
| 140 | LOG(ERROR) << "Received failure event from DHCP client."; |
| 141 | UpdateProperties(IPConfig::Properties(), false); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 144 | if (reason != kReasonBound && |
| 145 | reason != kReasonRebind && |
| 146 | reason != kReasonReboot && |
| 147 | reason != kReasonRenew) { |
| 148 | LOG(WARNING) << "Event ignored."; |
| 149 | return; |
| 150 | } |
| 151 | IPConfig::Properties properties; |
| 152 | CHECK(ParseConfiguration(configuration, &properties)); |
| 153 | UpdateProperties(properties, true); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 154 | } |
| 155 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 156 | void DHCPConfig::UpdateProperties(const Properties &properties, bool success) { |
| 157 | StopDHCPTimeout(); |
| 158 | IPConfig::UpdateProperties(properties, success); |
| 159 | } |
| 160 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 161 | bool DHCPConfig::Start() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 162 | SLOG(DHCP, 2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 163 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 164 | vector<char *> args; |
| 165 | args.push_back(const_cast<char *>(kDHCPCDPath)); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 166 | args.push_back(const_cast<char *>("-B")); // Run in foreground. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 167 | if (!request_hostname_.empty()) { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 168 | args.push_back(const_cast<char *>("-h")); // Request hostname from server. |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 169 | args.push_back(const_cast<char *>(request_hostname_.c_str())); |
| 170 | } |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 171 | if (arp_gateway_) { |
| 172 | args.push_back(const_cast<char *>("-R")); // ARP for default gateway. |
| 173 | } |
| 174 | string interface_arg(device_name()); |
| 175 | if (lease_file_suffix_ != device_name()) { |
| 176 | interface_arg = base::StringPrintf("%s=%s", device_name().c_str(), |
| 177 | lease_file_suffix_.c_str()); |
| 178 | } |
| 179 | args.push_back(const_cast<char *>(interface_arg.c_str())); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 180 | args.push_back(NULL); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame^] | 181 | |
| 182 | struct minijail *jail = minijail_->New(); |
| 183 | minijail_->DropRoot(jail, kDHCPCDUser); |
| 184 | minijail_->UseCapabilities(jail, |
| 185 | CAP_TO_MASK(CAP_NET_BIND_SERVICE) | |
| 186 | CAP_TO_MASK(CAP_NET_BROADCAST) | |
| 187 | CAP_TO_MASK(CAP_NET_ADMIN) | |
| 188 | CAP_TO_MASK(CAP_NET_RAW)); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 189 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 190 | CHECK(!pid_); |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame^] | 191 | if (!minijail_->RunAndDestroy(jail, args, &pid_)) { |
| 192 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath << " in a jail."; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 193 | return false; |
| 194 | } |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 195 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 196 | provider_->BindPID(pid_, this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 197 | CHECK(!child_watch_tag_); |
| 198 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 199 | StartDHCPTimeout(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 200 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 203 | void DHCPConfig::Stop() { |
| 204 | if (pid_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 205 | SLOG(DHCP, 2) << "Terminating " << pid_; |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 206 | if (kill(pid_, SIGTERM) < 0) { |
| 207 | PLOG(ERROR); |
| 208 | return; |
| 209 | } |
| 210 | pid_t ret; |
| 211 | int num_iterations = |
| 212 | kDHCPCDExitWaitMilliseconds / kDHCPCDExitPollMilliseconds; |
| 213 | for (int count = 0; count < num_iterations; ++count) { |
| 214 | ret = waitpid(pid_, NULL, WNOHANG); |
| 215 | if (ret == pid_ || ret == -1) |
| 216 | break; |
| 217 | usleep(kDHCPCDExitPollMilliseconds * 1000); |
| 218 | if (count == num_iterations / 2) // Make one last attempt to kill dhcpcd. |
| 219 | kill(pid_, SIGKILL); |
| 220 | } |
| 221 | if (ret != pid_) |
| 222 | PLOG(ERROR); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 223 | } |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 224 | StopDHCPTimeout(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 227 | bool DHCPConfig::Restart() { |
| 228 | // Check to ensure that this instance doesn't get destroyed in the middle of |
| 229 | // this call. If stopping a running client while there's only one reference to |
| 230 | // this instance, we will end up destroying it when the PID is unbound from |
| 231 | // the Provider. Since the Provider doesn't invoke Restart, this would mean |
| 232 | // that Restart was erroneously executed through a bare reference. |
| 233 | CHECK(!pid_ || !HasOneRef()); |
| 234 | Stop(); |
| 235 | if (pid_) { |
| 236 | provider_->UnbindPID(pid_); |
| 237 | } |
| 238 | CleanupClientState(); |
| 239 | return Start(); |
| 240 | } |
| 241 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 242 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 243 | char str[INET_ADDRSTRLEN]; |
| 244 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 245 | return str; |
| 246 | } |
| 247 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 248 | return ""; |
| 249 | } |
| 250 | |
| 251 | bool DHCPConfig::ParseConfiguration(const Configuration& configuration, |
| 252 | IPConfig::Properties *properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 253 | SLOG(DHCP, 2) << __func__; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame] | 254 | properties->method = flimflam::kTypeDHCP; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 255 | properties->address_family = IPAddress::kFamilyIPv4; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 256 | for (Configuration::const_iterator it = configuration.begin(); |
| 257 | it != configuration.end(); ++it) { |
| 258 | const string &key = it->first; |
| 259 | const DBus::Variant &value = it->second; |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 260 | SLOG(DHCP, 2) << "Processing key: " << key; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 261 | if (key == kConfigurationKeyIPAddress) { |
| 262 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 263 | if (properties->address.empty()) { |
| 264 | return false; |
| 265 | } |
| 266 | } else if (key == kConfigurationKeySubnetCIDR) { |
Paul Stewart | 48100b0 | 2012-03-19 07:53:52 -0700 | [diff] [blame] | 267 | properties->subnet_prefix = value.reader().get_byte(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 268 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 269 | properties->broadcast_address = |
| 270 | GetIPv4AddressString(value.reader().get_uint32()); |
| 271 | if (properties->broadcast_address.empty()) { |
| 272 | return false; |
| 273 | } |
| 274 | } else if (key == kConfigurationKeyRouters) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 275 | vector<unsigned int> routers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 276 | if (routers.empty()) { |
| 277 | LOG(ERROR) << "No routers provided."; |
| 278 | return false; |
| 279 | } |
| 280 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 281 | if (properties->gateway.empty()) { |
| 282 | return false; |
| 283 | } |
| 284 | } else if (key == kConfigurationKeyDNS) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 285 | vector<unsigned int> servers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 286 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 287 | it != servers.end(); ++it) { |
| 288 | string server = GetIPv4AddressString(*it); |
| 289 | if (server.empty()) { |
| 290 | return false; |
| 291 | } |
| 292 | properties->dns_servers.push_back(server); |
| 293 | } |
| 294 | } else if (key == kConfigurationKeyDomainName) { |
| 295 | properties->domain_name = value.reader().get_string(); |
| 296 | } else if (key == kConfigurationKeyDomainSearch) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 297 | properties->domain_search = value.operator vector<string>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 298 | } else if (key == kConfigurationKeyMTU) { |
| 299 | int mtu = value.reader().get_uint16(); |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 300 | if (mtu >= kMinMTU) { |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 301 | properties->mtu = mtu; |
| 302 | } |
| 303 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 304 | SLOG(DHCP, 2) << "Key ignored."; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 310 | void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 311 | SLOG(DHCP, 2) << "pid " << pid << " exit status " << status; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 312 | DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data); |
| 313 | config->child_watch_tag_ = 0; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 314 | CHECK_EQ(pid, config->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 315 | config->CleanupClientState(); |
| 316 | |
| 317 | // |config| instance may be destroyed after this call. |
| 318 | config->provider_->UnbindPID(pid); |
| 319 | } |
| 320 | |
| 321 | void DHCPConfig::CleanupClientState() { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 322 | if (child_watch_tag_) { |
| 323 | glib_->SourceRemove(child_watch_tag_); |
| 324 | child_watch_tag_ = 0; |
| 325 | } |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame^] | 326 | pid_ = 0; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 327 | proxy_.reset(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 328 | if (lease_file_suffix_ == device_name()) { |
| 329 | // If the lease file suffix was left as default, clean it up at exit. |
| 330 | file_util::Delete(root_.Append( |
| 331 | base::StringPrintf(kDHCPCDPathFormatLease, |
| 332 | device_name().c_str())), false); |
| 333 | } |
| 334 | file_util::Delete(root_.Append( |
| 335 | base::StringPrintf(kDHCPCDPathFormatPID, device_name().c_str())), false); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 336 | } |
| 337 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 338 | void DHCPConfig::StartDHCPTimeout() { |
| 339 | lease_acquisition_timeout_callback_.Reset( |
| 340 | Bind(&DHCPConfig::ProcessDHCPTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 341 | dispatcher_->PostDelayedTask( |
| 342 | lease_acquisition_timeout_callback_.callback(), |
| 343 | lease_acquisition_timeout_seconds_ * 1000); |
| 344 | } |
| 345 | |
| 346 | void DHCPConfig::StopDHCPTimeout() { |
| 347 | lease_acquisition_timeout_callback_.Cancel(); |
| 348 | } |
| 349 | |
| 350 | void DHCPConfig::ProcessDHCPTimeout() { |
| 351 | LOG(ERROR) << "Timed out waiting for DHCP lease on " << device_name() << " " |
| 352 | << "(after " << lease_acquisition_timeout_seconds_ << " seconds)."; |
| 353 | UpdateProperties(IPConfig::Properties(), false); |
| 354 | } |
| 355 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 356 | } // namespace shill |