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