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 | #ifndef SHILL_DHCP_CONFIG_ |
| 6 | #define SHILL_DHCP_CONFIG_ |
| 7 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 8 | #include <base/cancelable_callback.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 9 | #include <base/file_path.h> |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 10 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 11 | #include <base/memory/weak_ptr.h> |
Darin Petkov | aceede3 | 2011-07-18 15:32:38 -0700 | [diff] [blame] | 12 | #include <dbus-c++/types.h> |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 13 | #include <glib.h> |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 15 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 16 | #include "shill/ipconfig.h" |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 17 | #include "shill/minijail.h" |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 21 | class ControlInterface; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 22 | class DHCPProvider; |
| 23 | class DHCPProxyInterface; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 24 | class EventDispatcher; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 25 | class GLib; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 26 | class ProxyFactory; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 27 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 28 | // This class provides a DHCP client instance for the device |device_name|. |
| 29 | // If |request_hostname| is non-empty, it asks the DHCP server to register |
| 30 | // this hostname on our behalf, for purposes of administration or creating |
| 31 | // a dynamic DNS entry. |
| 32 | // |
| 33 | // The DHPCConfig instance asks the DHCP client to create a lease file |
| 34 | // containing the name |lease_file_suffix|. If this suffix is the same as |
| 35 | // |device_name|, the lease is considered to be ephemeral, and the lease |
| 36 | // file is removed whenever this DHCPConfig instance is no longer needed. |
| 37 | // Otherwise, the lease file persists and will be re-used in future attempts. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 38 | class DHCPConfig : public IPConfig { |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 40 | typedef std::map<std::string, DBus::Variant> Configuration; |
| 41 | |
Darin Petkov | 14c29ec | 2012-03-02 11:34:19 +0100 | [diff] [blame] | 42 | static const int kMinMTU; |
| 43 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 44 | DHCPConfig(ControlInterface *control_interface, |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 45 | EventDispatcher *dispatcher, |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 46 | DHCPProvider *provider, |
Darin Petkov | f65e928 | 2011-06-21 14:29:56 -0700 | [diff] [blame] | 47 | const std::string &device_name, |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 48 | const std::string &request_hostname, |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 49 | const std::string &lease_file_suffix, |
| 50 | bool arp_gateway, |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 51 | GLib *glib); |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 52 | virtual ~DHCPConfig(); |
| 53 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 54 | // Inherited from IPConfig. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 55 | virtual bool RequestIP(); |
| 56 | virtual bool RenewIP(); |
| 57 | virtual bool ReleaseIP(); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 58 | |
| 59 | // If |proxy_| is not initialized already, sets it to a new D-Bus proxy to |
| 60 | // |service|. |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 61 | void InitProxy(const std::string &service); |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 62 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 63 | // Processes an Event signal from dhcpcd. |
| 64 | void ProcessEventSignal(const std::string &reason, |
| 65 | const Configuration &configuration); |
| 66 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 67 | protected: |
| 68 | // Overrides base clase implementation. |
| 69 | virtual void UpdateProperties(const Properties &properties, bool success); |
| 70 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 71 | private: |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 72 | friend class DHCPConfigTest; |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 73 | FRIEND_TEST(DHCPConfigTest, GetIPv4AddressString); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 74 | FRIEND_TEST(DHCPConfigTest, InitProxy); |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 75 | FRIEND_TEST(DHCPConfigTest, ParseClasslessStaticRoutes); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 76 | FRIEND_TEST(DHCPConfigTest, ParseConfiguration); |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 77 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalFail); |
| 78 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalSuccess); |
| 79 | FRIEND_TEST(DHCPConfigTest, ProcessEventSignalUnknown); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 80 | FRIEND_TEST(DHCPConfigTest, ReleaseIP); |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 81 | FRIEND_TEST(DHCPConfigTest, ReleaseIPArpGW); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 82 | FRIEND_TEST(DHCPConfigTest, RenewIP); |
| 83 | FRIEND_TEST(DHCPConfigTest, RequestIP); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 84 | FRIEND_TEST(DHCPConfigTest, RequestIPTimeout); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 85 | FRIEND_TEST(DHCPConfigTest, Restart); |
| 86 | FRIEND_TEST(DHCPConfigTest, RestartNoClient); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 87 | FRIEND_TEST(DHCPConfigTest, StartFail); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 88 | FRIEND_TEST(DHCPConfigTest, StartTimeout); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 89 | FRIEND_TEST(DHCPConfigTest, StartWithHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 90 | FRIEND_TEST(DHCPConfigTest, StartWithoutArpGateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 91 | FRIEND_TEST(DHCPConfigTest, StartWithoutHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 92 | FRIEND_TEST(DHCPConfigTest, StartWithoutLeaseSuffix); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 93 | FRIEND_TEST(DHCPConfigTest, Stop); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 94 | FRIEND_TEST(DHCPConfigTest, StopDuringRequestIP); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 95 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 96 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 97 | static const char kConfigurationKeyBroadcastAddress[]; |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 98 | static const char kConfigurationKeyClasslessStaticRoutes[]; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 99 | static const char kConfigurationKeyDNS[]; |
| 100 | static const char kConfigurationKeyDomainName[]; |
| 101 | static const char kConfigurationKeyDomainSearch[]; |
| 102 | static const char kConfigurationKeyIPAddress[]; |
| 103 | static const char kConfigurationKeyMTU[]; |
| 104 | static const char kConfigurationKeyRouters[]; |
| 105 | static const char kConfigurationKeySubnetCIDR[]; |
| 106 | |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 107 | static const int kDHCPCDExitPollMilliseconds; |
| 108 | static const int kDHCPCDExitWaitMilliseconds; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 109 | static const char kDHCPCDPath[]; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 110 | static const char kDHCPCDPathFormatLease[]; |
| 111 | static const char kDHCPCDPathFormatPID[]; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 112 | static const int kDHCPTimeoutSeconds; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 113 | static const char kDHCPCDUser[]; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 114 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 115 | static const char kReasonBound[]; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 116 | static const char kReasonGatewayArp[]; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 117 | static const char kReasonFail[]; |
| 118 | static const char kReasonRebind[]; |
| 119 | static const char kReasonReboot[]; |
| 120 | static const char kReasonRenew[]; |
| 121 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 122 | static const char kType[]; |
| 123 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 124 | // Starts dhcpcd, returns true on success and false otherwise. |
| 125 | bool Start(); |
| 126 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 127 | // Stops dhcpcd if running. |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 128 | void Stop(const char *reason); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 129 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 130 | // Stops dhcpcd if already running and then starts it. Returns true on success |
| 131 | // and false otherwise. |
| 132 | bool Restart(); |
| 133 | |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 134 | // Parses |classless_routes| into |properties|. Sets the default gateway |
| 135 | // if one is supplied and |properties| does not already contain one. It |
| 136 | // also sets the "routes" parameter of the IPConfig properties for all |
| 137 | // routes not converted into the default gateway. Returns true on |
| 138 | // success, and false otherwise. |
| 139 | static bool ParseClasslessStaticRoutes(const std::string &classless_routes, |
| 140 | IPConfig::Properties *properties); |
| 141 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 142 | // Parses |configuration| into |properties|. Returns true on success, and |
| 143 | // false otherwise. |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 144 | bool ParseConfiguration(const Configuration &configuration, |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 145 | IPConfig::Properties *properties); |
| 146 | |
| 147 | // Returns the string representation of the IP address |address|, or an |
| 148 | // empty string on failure. |
| 149 | std::string GetIPv4AddressString(unsigned int address); |
| 150 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 151 | // Called when the dhcpcd client process exits. |
| 152 | static void ChildWatchCallback(GPid pid, gint status, gpointer data); |
| 153 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 154 | // Cleans up remaining state from a running client, if any, including freeing |
| 155 | // its GPid, exit watch callback, and state files. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 156 | void CleanupClientState(); |
| 157 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 158 | // Initialize a callback that will invoke ProcessDHCPTimeout if we |
| 159 | // do not get a lease in a reasonable amount of time. |
| 160 | void StartDHCPTimeout(); |
| 161 | // Cancel callback created by StartDHCPTimeout. One-liner included |
| 162 | // for symmetry. |
| 163 | void StopDHCPTimeout(); |
| 164 | // Called if we do not get a DHCP lease in a reasonable amount of time. |
| 165 | // Informs upper layers of the failure. |
| 166 | void ProcessDHCPTimeout(); |
| 167 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 168 | // Store cached copies of singletons for speed/ease of testing. |
| 169 | ProxyFactory *proxy_factory_; |
| 170 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 171 | DHCPProvider *provider_; |
| 172 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 173 | // Hostname to be used in the request. This will be passed to the DHCP |
| 174 | // server in the request. |
| 175 | std::string request_hostname_; |
| 176 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 177 | // DHCP lease file suffix, used to differentiate the lease of one interface |
| 178 | // or network from another. |
| 179 | std::string lease_file_suffix_; |
| 180 | |
| 181 | // Specifies whether to supply an argument to the DHCP client to validate |
| 182 | // the acquired IP address using an ARP request to the gateway IP address. |
| 183 | bool arp_gateway_; |
| 184 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 185 | // The PID of the spawned DHCP client. May be 0 if no client has been spawned |
| 186 | // yet or the client has died. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 187 | int pid_; |
| 188 | |
| 189 | // Child exit watch callback source tag. |
| 190 | unsigned int child_watch_tag_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 191 | |
| 192 | // The proxy for communicating with the DHCP client. |
| 193 | scoped_ptr<DHCPProxyInterface> proxy_; |
| 194 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 195 | // Called if we fail to get a DHCP lease in a timely manner. |
| 196 | base::CancelableClosure lease_acquisition_timeout_callback_; |
| 197 | |
| 198 | // Time to wait for a DHCP lease. Represented as field so that it |
| 199 | // can be overriden in tests. |
| 200 | unsigned int lease_acquisition_timeout_seconds_; |
| 201 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 202 | // Root file path, used for testing. |
| 203 | FilePath root_; |
| 204 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 205 | base::WeakPtrFactory<DHCPConfig> weak_ptr_factory_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 206 | EventDispatcher *dispatcher_; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 207 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 208 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 209 | Minijail *minijail_; |
| 210 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 211 | DISALLOW_COPY_AND_ASSIGN(DHCPConfig); |
| 212 | }; |
| 213 | |
| 214 | } // namespace shill |
| 215 | |
| 216 | #endif // SHILL_DHCP_CONFIG_ |