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