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(); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 57 | virtual bool ReleaseIP(ReleaseReason reason); |
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); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 82 | FRIEND_TEST(DHCPConfigTest, ReleaseIPStaticIPWithLease); |
| 83 | FRIEND_TEST(DHCPConfigTest, ReleaseIPStaticIPWithoutLease); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 84 | FRIEND_TEST(DHCPConfigTest, RenewIP); |
| 85 | FRIEND_TEST(DHCPConfigTest, RequestIP); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 86 | FRIEND_TEST(DHCPConfigTest, RequestIPTimeout); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 87 | FRIEND_TEST(DHCPConfigTest, Restart); |
| 88 | FRIEND_TEST(DHCPConfigTest, RestartNoClient); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 89 | FRIEND_TEST(DHCPConfigTest, StartFail); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 90 | FRIEND_TEST(DHCPConfigTest, StartTimeout); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 91 | FRIEND_TEST(DHCPConfigTest, StartWithHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 92 | FRIEND_TEST(DHCPConfigTest, StartWithoutArpGateway); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 93 | FRIEND_TEST(DHCPConfigTest, StartWithoutHostname); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 94 | FRIEND_TEST(DHCPConfigTest, StartWithoutLeaseSuffix); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 95 | FRIEND_TEST(DHCPConfigTest, Stop); |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 96 | FRIEND_TEST(DHCPConfigTest, StopDuringRequestIP); |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 97 | FRIEND_TEST(DHCPProviderTest, CreateConfig); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 98 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 99 | static const char kConfigurationKeyBroadcastAddress[]; |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 100 | static const char kConfigurationKeyClasslessStaticRoutes[]; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 101 | static const char kConfigurationKeyDNS[]; |
| 102 | static const char kConfigurationKeyDomainName[]; |
| 103 | static const char kConfigurationKeyDomainSearch[]; |
| 104 | static const char kConfigurationKeyIPAddress[]; |
| 105 | static const char kConfigurationKeyMTU[]; |
| 106 | static const char kConfigurationKeyRouters[]; |
| 107 | static const char kConfigurationKeySubnetCIDR[]; |
| 108 | |
Thieu Le | 94eed56 | 2012-02-21 15:57:29 -0800 | [diff] [blame] | 109 | static const int kDHCPCDExitPollMilliseconds; |
| 110 | static const int kDHCPCDExitWaitMilliseconds; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 111 | static const char kDHCPCDPath[]; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 112 | static const char kDHCPCDPathFormatPID[]; |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 113 | static const int kDHCPTimeoutSeconds; |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 114 | static const char kDHCPCDUser[]; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 115 | |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 116 | static const char kReasonBound[]; |
Paul Stewart | a02ee49 | 2012-05-16 10:04:53 -0700 | [diff] [blame] | 117 | static const char kReasonGatewayArp[]; |
Darin Petkov | f9b0ca8 | 2011-06-20 12:10:23 -0700 | [diff] [blame] | 118 | static const char kReasonFail[]; |
| 119 | static const char kReasonRebind[]; |
| 120 | static const char kReasonReboot[]; |
| 121 | static const char kReasonRenew[]; |
| 122 | |
Chris Masone | 0756f23 | 2011-07-21 17:24:00 -0700 | [diff] [blame] | 123 | static const char kType[]; |
| 124 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 125 | // Starts dhcpcd, returns true on success and false otherwise. |
| 126 | bool Start(); |
| 127 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 128 | // Stops dhcpcd if running. |
mukesh agrawal | 1835e77 | 2013-01-15 18:35:03 -0800 | [diff] [blame] | 129 | void Stop(const char *reason); |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 130 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 131 | // Stops dhcpcd if already running and then starts it. Returns true on success |
| 132 | // and false otherwise. |
| 133 | bool Restart(); |
| 134 | |
Paul Stewart | 65bcd08 | 2012-11-16 09:37:14 -0800 | [diff] [blame] | 135 | // Parses |classless_routes| into |properties|. Sets the default gateway |
| 136 | // if one is supplied and |properties| does not already contain one. It |
| 137 | // also sets the "routes" parameter of the IPConfig properties for all |
| 138 | // routes not converted into the default gateway. Returns true on |
| 139 | // success, and false otherwise. |
| 140 | static bool ParseClasslessStaticRoutes(const std::string &classless_routes, |
| 141 | IPConfig::Properties *properties); |
| 142 | |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 143 | // Parses |configuration| into |properties|. Returns true on success, and |
| 144 | // false otherwise. |
mukesh agrawal | 28fd88e | 2013-05-02 14:06:10 -0700 | [diff] [blame] | 145 | static bool ParseConfiguration(const Configuration &configuration, |
| 146 | IPConfig::Properties *properties); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 147 | |
| 148 | // Returns the string representation of the IP address |address|, or an |
| 149 | // empty string on failure. |
mukesh agrawal | 28fd88e | 2013-05-02 14:06:10 -0700 | [diff] [blame] | 150 | static std::string GetIPv4AddressString(unsigned int address); |
Darin Petkov | e7cb7f8 | 2011-06-03 13:21:51 -0700 | [diff] [blame] | 151 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 152 | // Called when the dhcpcd client process exits. |
| 153 | static void ChildWatchCallback(GPid pid, gint status, gpointer data); |
| 154 | |
Darin Petkov | 98dd6a0 | 2011-06-10 15:12:57 -0700 | [diff] [blame] | 155 | // Cleans up remaining state from a running client, if any, including freeing |
| 156 | // its GPid, exit watch callback, and state files. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 157 | void CleanupClientState(); |
| 158 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 159 | // Initialize a callback that will invoke ProcessDHCPTimeout if we |
| 160 | // do not get a lease in a reasonable amount of time. |
| 161 | void StartDHCPTimeout(); |
| 162 | // Cancel callback created by StartDHCPTimeout. One-liner included |
| 163 | // for symmetry. |
| 164 | void StopDHCPTimeout(); |
| 165 | // Called if we do not get a DHCP lease in a reasonable amount of time. |
| 166 | // Informs upper layers of the failure. |
| 167 | void ProcessDHCPTimeout(); |
| 168 | |
Darin Petkov | 3fe1766 | 2013-02-04 14:19:08 +0100 | [diff] [blame] | 169 | void KillClient(); |
| 170 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 171 | // Store cached copies of singletons for speed/ease of testing. |
| 172 | ProxyFactory *proxy_factory_; |
| 173 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 174 | DHCPProvider *provider_; |
| 175 | |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 176 | // Hostname to be used in the request. This will be passed to the DHCP |
| 177 | // server in the request. |
| 178 | std::string request_hostname_; |
| 179 | |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 180 | // DHCP lease file suffix, used to differentiate the lease of one interface |
| 181 | // or network from another. |
| 182 | std::string lease_file_suffix_; |
| 183 | |
| 184 | // Specifies whether to supply an argument to the DHCP client to validate |
| 185 | // the acquired IP address using an ARP request to the gateway IP address. |
| 186 | bool arp_gateway_; |
| 187 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 188 | // The PID of the spawned DHCP client. May be 0 if no client has been spawned |
| 189 | // yet or the client has died. |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 190 | int pid_; |
| 191 | |
| 192 | // Child exit watch callback source tag. |
| 193 | unsigned int child_watch_tag_; |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 194 | |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 195 | // Whether a lease has been acquired from the DHCP server or gateway ARP. |
| 196 | bool is_lease_active_; |
| 197 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 198 | // The proxy for communicating with the DHCP client. |
| 199 | scoped_ptr<DHCPProxyInterface> proxy_; |
| 200 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 201 | // Called if we fail to get a DHCP lease in a timely manner. |
| 202 | base::CancelableClosure lease_acquisition_timeout_callback_; |
| 203 | |
| 204 | // Time to wait for a DHCP lease. Represented as field so that it |
| 205 | // can be overriden in tests. |
| 206 | unsigned int lease_acquisition_timeout_seconds_; |
| 207 | |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 208 | // Root file path, used for testing. |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 209 | base::FilePath root_; |
Darin Petkov | 92c4390 | 2011-06-09 20:46:06 -0700 | [diff] [blame] | 210 | |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 211 | base::WeakPtrFactory<DHCPConfig> weak_ptr_factory_; |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 212 | EventDispatcher *dispatcher_; |
Darin Petkov | 3258a81 | 2011-06-23 11:28:45 -0700 | [diff] [blame] | 213 | GLib *glib_; |
Darin Petkov | f7897bc | 2011-06-08 17:13:36 -0700 | [diff] [blame] | 214 | |
Jorge Lucangeli Obes | ad43cc6 | 2012-04-11 16:25:43 -0700 | [diff] [blame] | 215 | Minijail *minijail_; |
| 216 | |
Darin Petkov | 50308cd | 2011-06-01 18:25:07 -0700 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(DHCPConfig); |
| 218 | }; |
| 219 | |
| 220 | } // namespace shill |
| 221 | |
| 222 | #endif // SHILL_DHCP_CONFIG_ |