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