Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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> |
| 8 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 9 | #include <base/file_util.h> |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 10 | #include <base/logging.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" |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 16 | #include "shill/glib.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 17 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 21 | namespace shill { |
| 22 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 23 | const char DHCPConfig::kConfigurationKeyBroadcastAddress[] = "BroadcastAddress"; |
| 24 | const char DHCPConfig::kConfigurationKeyDNS[] = "DomainNameServers"; |
| 25 | const char DHCPConfig::kConfigurationKeyDomainName[] = "DomainName"; |
| 26 | const char DHCPConfig::kConfigurationKeyDomainSearch[] = "DomainSearch"; |
| 27 | const char DHCPConfig::kConfigurationKeyIPAddress[] = "IPAddress"; |
| 28 | const char DHCPConfig::kConfigurationKeyMTU[] = "InterfaceMTU"; |
| 29 | const char DHCPConfig::kConfigurationKeyRouters[] = "Routers"; |
| 30 | const char DHCPConfig::kConfigurationKeySubnetCIDR[] = "SubnetCIDR"; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 31 | const char DHCPConfig::kDHCPCDPath[] = "/sbin/dhcpcd"; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 32 | const char DHCPConfig::kDHCPCDPathFormatLease[] = "var/run/dhcpcd-%s.lease"; |
| 33 | const char DHCPConfig::kDHCPCDPathFormatPID[] = "var/run/dhcpcd-%s.pid"; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 34 | const char DHCPConfig::kReasonBound[] = "BOUND"; |
| 35 | const char DHCPConfig::kReasonFail[] = "FAIL"; |
| 36 | const char DHCPConfig::kReasonRebind[] = "REBIND"; |
| 37 | const char DHCPConfig::kReasonReboot[] = "REBOOT"; |
| 38 | const char DHCPConfig::kReasonRenew[] = "RENEW"; |
| 39 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 41 | DHCPConfig::DHCPConfig(DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 42 | const string &device_name, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 43 | GLib *glib) |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 44 | : IPConfig(device_name), |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 45 | provider_(provider), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 46 | pid_(0), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 47 | child_watch_tag_(0), |
| 48 | root_("/"), |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 49 | glib_(glib) { |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame^] | 50 | RegisterConstString(flimflam::kAddressProperty, &(properties().address)); |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 51 | VLOG(2) << __func__ << ": " << device_name; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | DHCPConfig::~DHCPConfig() { |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 55 | VLOG(2) << __func__ << ": " << device_name(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 56 | |
| 57 | // Don't leave behind dhcpcd running. |
| 58 | Stop(); |
| 59 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 60 | // Make sure we don't get any callbacks to the destroyed instance. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 61 | CleanupClientState(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 64 | bool DHCPConfig::RequestIP() { |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 65 | VLOG(2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 66 | if (!pid_) { |
| 67 | return Start(); |
| 68 | } |
| 69 | if (!proxy_.get()) { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 70 | LOG(ERROR) << "Unable to request IP before acquiring destination."; |
| 71 | return Restart(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 72 | } |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 73 | return RenewIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 76 | bool DHCPConfig::RenewIP() { |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 77 | VLOG(2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 78 | if (!pid_) { |
| 79 | return false; |
| 80 | } |
| 81 | if (!proxy_.get()) { |
| 82 | LOG(ERROR) << "Unable to renew IP before acquiring destination."; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 85 | proxy_->DoRebind(device_name()); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 89 | bool DHCPConfig::ReleaseIP() { |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 90 | VLOG(2) << __func__ << ": " << device_name(); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 91 | if (!pid_) { |
| 92 | return true; |
| 93 | } |
| 94 | if (!proxy_.get()) { |
| 95 | LOG(ERROR) << "Unable to release IP before acquiring destination."; |
| 96 | return false; |
| 97 | } |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 98 | proxy_->DoRelease(device_name()); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 99 | Stop(); |
| 100 | return true; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 103 | void DHCPConfig::InitProxy(DBus::Connection *connection, const char *service) { |
| 104 | if (!proxy_.get()) { |
| 105 | proxy_.reset(new DHCPCDProxy(connection, service)); |
| 106 | } |
| 107 | } |
| 108 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 109 | void DHCPConfig::ProcessEventSignal(const string &reason, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 110 | const Configuration &configuration) { |
| 111 | LOG(INFO) << "Event reason: " << reason; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 112 | if (reason == kReasonFail) { |
| 113 | LOG(ERROR) << "Received failure event from DHCP client."; |
| 114 | UpdateProperties(IPConfig::Properties(), false); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 117 | if (reason != kReasonBound && |
| 118 | reason != kReasonRebind && |
| 119 | reason != kReasonReboot && |
| 120 | reason != kReasonRenew) { |
| 121 | LOG(WARNING) << "Event ignored."; |
| 122 | return; |
| 123 | } |
| 124 | IPConfig::Properties properties; |
| 125 | CHECK(ParseConfiguration(configuration, &properties)); |
| 126 | UpdateProperties(properties, true); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 129 | bool DHCPConfig::Start() { |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 130 | VLOG(2) << __func__ << ": " << device_name(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 131 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 132 | char *argv[4] = { |
| 133 | const_cast<char *>(kDHCPCDPath), |
| 134 | const_cast<char *>("-B"), // foreground |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 135 | const_cast<char *>(device_name().c_str()), |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 136 | NULL |
| 137 | }; |
| 138 | char *envp[1] = { NULL }; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 139 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 140 | CHECK(!pid_); |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 141 | if (!glib_->SpawnAsync(NULL, |
| 142 | argv, |
| 143 | envp, |
| 144 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 145 | NULL, |
| 146 | NULL, |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 147 | &pid_, |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 148 | NULL)) { |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 149 | LOG(ERROR) << "Unable to spawn " << kDHCPCDPath; |
| 150 | return false; |
| 151 | } |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 152 | LOG(INFO) << "Spawned " << kDHCPCDPath << " with pid: " << pid_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 153 | provider_->BindPID(pid_, this); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 154 | CHECK(!child_watch_tag_); |
| 155 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, ChildWatchCallback, this); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 156 | return true; |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 159 | void DHCPConfig::Stop() { |
| 160 | if (pid_) { |
| 161 | VLOG(2) << "Terminating " << pid_; |
| 162 | PLOG_IF(ERROR, kill(pid_, SIGTERM) < 0); |
| 163 | } |
| 164 | } |
| 165 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 166 | bool DHCPConfig::Restart() { |
| 167 | // Check to ensure that this instance doesn't get destroyed in the middle of |
| 168 | // this call. If stopping a running client while there's only one reference to |
| 169 | // this instance, we will end up destroying it when the PID is unbound from |
| 170 | // the Provider. Since the Provider doesn't invoke Restart, this would mean |
| 171 | // that Restart was erroneously executed through a bare reference. |
| 172 | CHECK(!pid_ || !HasOneRef()); |
| 173 | Stop(); |
| 174 | if (pid_) { |
| 175 | provider_->UnbindPID(pid_); |
| 176 | } |
| 177 | CleanupClientState(); |
| 178 | return Start(); |
| 179 | } |
| 180 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 181 | string DHCPConfig::GetIPv4AddressString(unsigned int address) { |
| 182 | char str[INET_ADDRSTRLEN]; |
| 183 | if (inet_ntop(AF_INET, &address, str, arraysize(str))) { |
| 184 | return str; |
| 185 | } |
| 186 | LOG(ERROR) << "Unable to convert IPv4 address to string: " << address; |
| 187 | return ""; |
| 188 | } |
| 189 | |
| 190 | bool DHCPConfig::ParseConfiguration(const Configuration& configuration, |
| 191 | IPConfig::Properties *properties) { |
| 192 | VLOG(2) << __func__; |
Chris Masone | 43b48a1 | 2011-07-01 13:37:07 -0700 | [diff] [blame^] | 193 | properties->method = flimflam::kTypeDHCP; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 194 | properties->address_family = IPConfig::kAddressFamilyIPv4; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 195 | for (Configuration::const_iterator it = configuration.begin(); |
| 196 | it != configuration.end(); ++it) { |
| 197 | const string &key = it->first; |
| 198 | const DBus::Variant &value = it->second; |
| 199 | VLOG(2) << "Processing key: " << key; |
| 200 | if (key == kConfigurationKeyIPAddress) { |
| 201 | properties->address = GetIPv4AddressString(value.reader().get_uint32()); |
| 202 | if (properties->address.empty()) { |
| 203 | return false; |
| 204 | } |
| 205 | } else if (key == kConfigurationKeySubnetCIDR) { |
| 206 | properties->subnet_cidr = value.reader().get_byte(); |
| 207 | } else if (key == kConfigurationKeyBroadcastAddress) { |
| 208 | properties->broadcast_address = |
| 209 | GetIPv4AddressString(value.reader().get_uint32()); |
| 210 | if (properties->broadcast_address.empty()) { |
| 211 | return false; |
| 212 | } |
| 213 | } else if (key == kConfigurationKeyRouters) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 214 | vector<unsigned int> routers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 215 | if (routers.empty()) { |
| 216 | LOG(ERROR) << "No routers provided."; |
| 217 | return false; |
| 218 | } |
| 219 | properties->gateway = GetIPv4AddressString(routers[0]); |
| 220 | if (properties->gateway.empty()) { |
| 221 | return false; |
| 222 | } |
| 223 | } else if (key == kConfigurationKeyDNS) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 224 | vector<unsigned int> servers = value.operator vector<unsigned int>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 225 | for (vector<unsigned int>::const_iterator it = servers.begin(); |
| 226 | it != servers.end(); ++it) { |
| 227 | string server = GetIPv4AddressString(*it); |
| 228 | if (server.empty()) { |
| 229 | return false; |
| 230 | } |
| 231 | properties->dns_servers.push_back(server); |
| 232 | } |
| 233 | } else if (key == kConfigurationKeyDomainName) { |
| 234 | properties->domain_name = value.reader().get_string(); |
| 235 | } else if (key == kConfigurationKeyDomainSearch) { |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 236 | properties->domain_search = value.operator vector<string>(); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 237 | } else if (key == kConfigurationKeyMTU) { |
| 238 | int mtu = value.reader().get_uint16(); |
| 239 | if (mtu >= 576) { |
| 240 | properties->mtu = mtu; |
| 241 | } |
| 242 | } else { |
| 243 | VLOG(2) << "Key ignored."; |
| 244 | } |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 249 | void DHCPConfig::ChildWatchCallback(GPid pid, gint status, gpointer data) { |
| 250 | VLOG(2) << "pid " << pid << " exit status " << status; |
| 251 | DHCPConfig *config = reinterpret_cast<DHCPConfig *>(data); |
| 252 | config->child_watch_tag_ = 0; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 253 | CHECK_EQ(pid, config->pid_); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 254 | config->CleanupClientState(); |
| 255 | |
| 256 | // |config| instance may be destroyed after this call. |
| 257 | config->provider_->UnbindPID(pid); |
| 258 | } |
| 259 | |
| 260 | void DHCPConfig::CleanupClientState() { |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 261 | if (child_watch_tag_) { |
| 262 | glib_->SourceRemove(child_watch_tag_); |
| 263 | child_watch_tag_ = 0; |
| 264 | } |
| 265 | if (pid_) { |
| 266 | glib_->SpawnClosePID(pid_); |
| 267 | pid_ = 0; |
| 268 | } |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 269 | proxy_.reset(); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 270 | file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatLease, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 271 | device_name().c_str())), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 272 | false); |
| 273 | file_util::Delete(root_.Append(base::StringPrintf(kDHCPCDPathFormatPID, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 274 | device_name().c_str())), |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 275 | false); |
| 276 | } |
| 277 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 278 | } // namespace shill |