blob: 87321a9ef467c7305847700088a5b44498f077f5 [file] [log] [blame]
Thieu Lecaef8932012-02-28 16:06:59 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove02b3ca2011-05-31 16:00:44 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_IPCONFIG_H_
6#define SHILL_IPCONFIG_H_
Darin Petkove02b3ca2011-05-31 16:00:44 -07007
Ben Chancd477322014-10-17 14:19:30 -07008#include <memory>
Darin Petkove02b3ca2011-05-31 16:00:44 -07009#include <string>
Samuel Tan815a6fb2014-10-23 16:53:59 -070010#include <sys/time.h>
Darin Petkove7cb7f82011-06-03 13:21:51 -070011#include <vector>
Darin Petkove02b3ca2011-05-31 16:00:44 -070012
Eric Shienbrood3e20a232012-02-16 11:35:56 -050013#include <base/callback.h>
Darin Petkove02b3ca2011-05-31 16:00:44 -070014#include <base/memory/ref_counted.h>
Darin Petkovefb09c32011-06-07 20:24:17 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkove02b3ca2011-05-31 16:00:44 -070016
Peter Qiu8d6b5972014-10-28 15:33:34 -070017#include "shill/net/ip_address.h"
Chris Masonec6c6c132011-06-30 11:29:52 -070018#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
Darin Petkov60596742012-03-05 12:17:17 +010020#include "shill/routing_table_entry.h"
Chris Masone2b105542011-06-22 10:58:09 -070021
Chris Masonec1e50412011-06-07 13:04:53 -070022namespace shill {
Chris Masonec6c6c132011-06-30 11:29:52 -070023class ControlInterface;
24class Error;
25class IPConfigAdaptorInterface;
Paul Stewart1062d9d2012-04-27 10:42:27 -070026class StaticIPParameters;
Samuel Tan815a6fb2014-10-23 16:53:59 -070027class Time;
Darin Petkove02b3ca2011-05-31 16:00:44 -070028
29// IPConfig superclass. Individual IP configuration types will inherit from this
30// class.
Chris Masone27c4aa52011-07-02 13:10:14 -070031class IPConfig : public base::RefCounted<IPConfig> {
Darin Petkove02b3ca2011-05-31 16:00:44 -070032 public:
Darin Petkov60596742012-03-05 12:17:17 +010033 struct Route {
34 std::string host;
35 std::string netmask;
36 std::string gateway;
37 };
38
Paul Stewartc39f1132011-06-22 12:02:28 -070039 struct Properties {
Paul Stewart7355ce12011-09-02 10:47:01 -070040 Properties() : address_family(IPAddress::kFamilyUnknown),
Paul Stewart48100b02012-03-19 07:53:52 -070041 subnet_prefix(0),
Peter Qiub255a762015-06-10 10:09:12 -070042 delegated_prefix_length(0),
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +010043 user_traffic_only(false),
Prabhu Kaliamoorthi1f589032015-02-23 13:28:20 +010044 default_route(true),
Ben Chana0163122012-09-25 15:10:52 -070045 blackhole_ipv6(false),
Paul Stewart024a6c82015-01-23 14:59:40 -080046 mtu(kUndefinedMTU),
Paul Stewart1f916e42013-12-23 09:52:54 -080047 lease_duration_seconds(0) {}
Paul Stewartc39f1132011-06-22 12:02:28 -070048
Paul Stewart1d18e8c2011-07-15 11:00:31 -070049 IPAddress::Family address_family;
Darin Petkove7cb7f82011-06-03 13:21:51 -070050 std::string address;
Ben Chan7fab8972014-08-10 17:14:46 -070051 int32_t subnet_prefix;
Darin Petkove7cb7f82011-06-03 13:21:51 -070052 std::string broadcast_address;
Darin Petkove7cb7f82011-06-03 13:21:51 -070053 std::vector<std::string> dns_servers;
54 std::string domain_name;
Matthew Wein08add482015-04-20 13:26:48 -070055 std::string accepted_hostname;
Darin Petkove7cb7f82011-06-03 13:21:51 -070056 std::vector<std::string> domain_search;
Chris Masone43b48a12011-07-01 13:37:07 -070057 std::string gateway;
58 std::string method;
59 std::string peer_address;
Peter Qiub255a762015-06-10 10:09:12 -070060 // IPv6 prefix delegated from a DHCPv6 server.
61 std::string delegated_prefix;
62 int32_t delegated_prefix_length;
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +010063 // Set the flag when a secondary routing table should be used for less
64 // privileged user traffic which alone would be sent to the VPN client. A
65 // primary routing table will be used for traffic from privileged processes
66 // which will bypass VPN.
67 bool user_traffic_only;
Prabhu Kaliamoorthi1f589032015-02-23 13:28:20 +010068 // Set the flag to true when the interface should be set as the default
69 // route.
70 bool default_route;
Prabhu Kaliamoorthi762bfb82015-02-06 13:17:08 +010071 // A list of IP blocks in CIDR format that should be excluded from VPN.
72 std::vector<std::string> exclusion_list;
Ben Chana0163122012-09-25 15:10:52 -070073 bool blackhole_ipv6;
Ben Chan7fab8972014-08-10 17:14:46 -070074 int32_t mtu;
Darin Petkov60596742012-03-05 12:17:17 +010075 std::vector<Route> routes;
Paul Stewartc3fdba92013-12-02 11:12:38 -080076 // Vendor encapsulated option string gained from DHCP.
77 std::string vendor_encapsulated_options;
Paul Stewarta63f5212013-06-25 15:29:40 -070078 // Web Proxy Auto Discovery (WPAD) URL gained from DHCP.
79 std::string web_proxy_auto_discovery;
Paul Stewart1f916e42013-12-23 09:52:54 -080080 // Length of time the lease was granted.
Ben Chan7fab8972014-08-10 17:14:46 -070081 uint32_t lease_duration_seconds;
Darin Petkove7cb7f82011-06-03 13:21:51 -070082 };
83
Ben Chan26692bd2014-02-03 16:34:55 -080084 enum Method {
85 kMethodUnknown,
86 kMethodPPP,
87 kMethodStatic,
88 kMethodDHCP
89 };
90
Paul Stewart217c61d2013-06-13 15:12:02 -070091 enum ReleaseReason {
92 kReleaseReasonDisconnect,
93 kReleaseReasonStaticIP
94 };
95
Samuel Tan3c3c36a2014-12-16 16:53:19 -080096 typedef base::Callback<void(const IPConfigRefPtr&, bool)> UpdateCallback;
Paul Stewartc5099532013-12-12 07:53:15 -080097 typedef base::Callback<void(const IPConfigRefPtr&)> Callback;
98
Paul Stewart024a6c82015-01-23 14:59:40 -080099 // Define a default and a minimum viable MTU value.
100 static const int kDefaultMTU;
101 static const int kMinIPv4MTU;
102 static const int kMinIPv6MTU;
103 static const int kUndefinedMTU;
104
Paul Stewart8ae18742015-06-16 13:13:10 -0700105 IPConfig(ControlInterface* control_interface, const std::string& device_name);
106 IPConfig(ControlInterface* control_interface,
107 const std::string& device_name,
108 const std::string& type);
Darin Petkove02b3ca2011-05-31 16:00:44 -0700109 virtual ~IPConfig();
110
Paul Stewart8ae18742015-06-16 13:13:10 -0700111 const std::string& device_name() const { return device_name_; }
112 const std::string& type() const { return type_; }
Chris Masone0756f232011-07-21 17:24:00 -0700113 uint serial() const { return serial_; }
Darin Petkove02b3ca2011-05-31 16:00:44 -0700114
Chris Masone4e851612011-07-01 10:46:53 -0700115 std::string GetRpcIdentifier();
116
Darin Petkovefb09c32011-06-07 20:24:17 -0700117 // Registers a callback that's executed every time the configuration
Paul Stewartc5099532013-12-12 07:53:15 -0800118 // properties are acquired. Takes ownership of |callback|. Pass NULL
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800119 // to remove a callback. The callback's first argument is a pointer to this IP
Darin Petkovf9b0ca82011-06-20 12:10:23 -0700120 // configuration instance allowing clients to more easily manage multiple IP
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800121 // configurations. The callback's second argument is a boolean indicating
122 // whether or not a DHCP lease was acquired from the server.
Paul Stewart8ae18742015-06-16 13:13:10 -0700123 void RegisterUpdateCallback(const UpdateCallback& callback);
Paul Stewartc5099532013-12-12 07:53:15 -0800124
125 // Registers a callback that's executed every time the configuration
126 // properties fail to be acquired. Takes ownership of |callback|. Pass NULL
127 // to remove a callback. The callback's argument is a pointer to this IP
128 // configuration instance allowing clients to more easily manage multiple IP
129 // configurations.
Paul Stewart8ae18742015-06-16 13:13:10 -0700130 void RegisterFailureCallback(const Callback& callback);
Darin Petkove7cb7f82011-06-03 13:21:51 -0700131
Paul Stewart82236532013-12-10 15:33:11 -0800132 // Registers a callback that's executed every time the Refresh method
133 // on the ipconfig is called. Takes ownership of |callback|. Pass NULL
134 // to remove a callback. The callback's argument is a pointer to this IP
135 // configuration instance allowing clients to more easily manage multiple IP
136 // configurations.
Paul Stewart8ae18742015-06-16 13:13:10 -0700137 void RegisterRefreshCallback(const Callback& callback);
Paul Stewart82236532013-12-10 15:33:11 -0800138
Paul Stewart1f916e42013-12-23 09:52:54 -0800139 // Registers a callback that's executed every time the the lease exipres
140 // and the IPConfig is about to perform a restart to attempt to regain it.
141 // Takes ownership of |callback|. Pass NULL to remove a callback. The
142 // callback's argument is a pointer to this IP configuration instance
143 // allowing clients to more easily manage multiple IP configurations.
Paul Stewart8ae18742015-06-16 13:13:10 -0700144 void RegisterExpireCallback(const Callback& callback);
Paul Stewart1f916e42013-12-23 09:52:54 -0800145
Paul Stewart8ae18742015-06-16 13:13:10 -0700146 void set_properties(const Properties& props) { properties_ = props; }
147 virtual const Properties& properties() const { return properties_; }
Darin Petkove7cb7f82011-06-03 13:21:51 -0700148
Peter Qiua89154b2014-05-23 15:45:42 -0700149 // Update DNS servers setting for this ipconfig, this allows Chrome
150 // to retrieve the new DNS servers.
Paul Stewart8ae18742015-06-16 13:13:10 -0700151 virtual void UpdateDNSServers(const std::vector<std::string>& dns_servers);
Peter Qiua89154b2014-05-23 15:45:42 -0700152
Paul Stewartc5099532013-12-12 07:53:15 -0800153 // Reset the IPConfig properties to their default values.
154 virtual void ResetProperties();
155
Darin Petkov92c43902011-06-09 20:46:06 -0700156 // Request, renew and release IP configuration. Return true on success, false
Darin Petkove7cb7f82011-06-03 13:21:51 -0700157 // otherwise. The default implementation always returns false indicating a
Paul Stewarta02ee492012-05-16 10:04:53 -0700158 // failure. ReleaseIP is advisory: if we are no longer connected, it is not
159 // possible to properly vacate the lease on the remote server. Also,
160 // depending on the configuration of the specific IPConfig subclass, we may
161 // end up holding on to the lease so we can resume to the network lease
162 // faster.
Darin Petkov92c43902011-06-09 20:46:06 -0700163 virtual bool RequestIP();
164 virtual bool RenewIP();
Paul Stewart217c61d2013-06-13 15:12:02 -0700165 virtual bool ReleaseIP(ReleaseReason reason);
Darin Petkovd1b715b2011-06-02 21:21:22 -0700166
Paul Stewart4558bda2012-08-03 10:44:10 -0700167 // Refresh IP configuration. Called by the DBus Adaptor "Refresh" call.
Paul Stewart8ae18742015-06-16 13:13:10 -0700168 void Refresh(Error* error);
Paul Stewart4558bda2012-08-03 10:44:10 -0700169
Paul Stewart8ae18742015-06-16 13:13:10 -0700170 PropertyStore* mutable_store() { return &store_; }
171 const PropertyStore& store() const { return store_; }
172 void ApplyStaticIPParameters(StaticIPParameters* static_ip_parameters);
Chris Masone43b48a12011-07-01 13:37:07 -0700173
Paul Stewart82236532013-12-10 15:33:11 -0800174 // Restore the fields of |properties_| to their original values before
175 // static IP parameters were previously applied.
Paul Stewart8ae18742015-06-16 13:13:10 -0700176 void RestoreSavedIPParameters(StaticIPParameters* static_ip_parameters);
Paul Stewart82236532013-12-10 15:33:11 -0800177
Samuel Tan815a6fb2014-10-23 16:53:59 -0700178 // Updates |current_lease_expiration_time_| by adding |new_lease_duration| to
179 // the current time.
180 virtual void UpdateLeaseExpirationTime(uint32_t new_lease_duration);
181
182 // Resets |current_lease_expiration_time_| to its default value.
183 virtual void ResetLeaseExpirationTime();
184
185 // Returns the time left (in seconds) till the current DHCP lease is to be
186 // renewed in |time_left|. Returns false if an error occurs (i.e. current
187 // lease has already expired or no current DHCP lease), true otherwise.
Paul Stewart8ae18742015-06-16 13:13:10 -0700188 bool TimeToLeaseExpiry(uint32_t* time_left);
Samuel Tan815a6fb2014-10-23 16:53:59 -0700189
Darin Petkovefb09c32011-06-07 20:24:17 -0700190 protected:
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700191 // Inform RPC listeners of changes to our properties. MAY emit
192 // changes even on unchanged properties.
193 virtual void EmitChanges();
194
Darin Petkovefb09c32011-06-07 20:24:17 -0700195 // Updates the IP configuration properties and notifies registered listeners
Paul Stewartc5099532013-12-12 07:53:15 -0800196 // about the event.
Paul Stewart8ae18742015-06-16 13:13:10 -0700197 virtual void UpdateProperties(const Properties& properties,
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800198 bool new_lease_acquired);
Paul Stewartc5099532013-12-12 07:53:15 -0800199
200 // Notifies registered listeners that the configuration process has failed.
201 virtual void NotifyFailure();
Darin Petkovefb09c32011-06-07 20:24:17 -0700202
Paul Stewart1f916e42013-12-23 09:52:54 -0800203 // Notifies registered listeners that the lease has expired.
204 virtual void NotifyExpiry();
205
Darin Petkove02b3ca2011-05-31 16:00:44 -0700206 private:
Chris Masonec6c6c132011-06-30 11:29:52 -0700207 friend class IPConfigAdaptorInterface;
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700208 friend class IPConfigTest;
Paul Stewartdd60e452011-08-08 11:38:36 -0700209 friend class ConnectionTest;
Chris Masonec6c6c132011-06-30 11:29:52 -0700210
Paul Stewart2bf1d352011-12-06 15:02:55 -0800211 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700212 FRIEND_TEST(DeviceTest, DestroyIPConfig);
Paul Stewartfa11e282013-12-02 22:04:25 -0800213 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Paul Stewart1f916e42013-12-23 09:52:54 -0800214 FRIEND_TEST(DeviceTest, OnIPConfigExpired);
Darin Petkovefb09c32011-06-07 20:24:17 -0700215 FRIEND_TEST(IPConfigTest, UpdateCallback);
216 FRIEND_TEST(IPConfigTest, UpdateProperties);
Samuel Tan815a6fb2014-10-23 16:53:59 -0700217 FRIEND_TEST(IPConfigTest, UpdateLeaseExpirationTime);
218 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_NoDHCPLease);
219 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_CurrentLeaseExpired);
220 FRIEND_TEST(IPConfigTest, TimeToLeaseExpiry_Success);
Paul Stewartb6063942011-08-05 10:17:29 -0700221 FRIEND_TEST(ResolverTest, Empty);
222 FRIEND_TEST(ResolverTest, NonEmpty);
Paul Stewart3f68bb12012-03-15 13:33:10 -0700223 FRIEND_TEST(RoutingTableTest, ConfigureRoutes);
Paul Stewart75e89d22011-08-01 10:00:02 -0700224 FRIEND_TEST(RoutingTableTest, RouteAddDelete);
Thieu Lecaef8932012-02-28 16:06:59 -0800225 FRIEND_TEST(RoutingTableTest, RouteDeleteForeign);
Darin Petkovefb09c32011-06-07 20:24:17 -0700226
Chris Masone0756f232011-07-21 17:24:00 -0700227 static const char kType[];
Chris Masone0756f232011-07-21 17:24:00 -0700228
mukesh agrawal7aed61c2013-04-22 16:01:24 -0700229 void Init();
230
231 static uint global_serial_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700232 PropertyStore store_;
Darin Petkovf65e9282011-06-21 14:29:56 -0700233 const std::string device_name_;
Chris Masone0756f232011-07-21 17:24:00 -0700234 const std::string type_;
235 const uint serial_;
Ben Chancd477322014-10-17 14:19:30 -0700236 std::unique_ptr<IPConfigAdaptorInterface> adaptor_;
Darin Petkove7cb7f82011-06-03 13:21:51 -0700237 Properties properties_;
Samuel Tan3c3c36a2014-12-16 16:53:19 -0800238 UpdateCallback update_callback_;
Paul Stewartc5099532013-12-12 07:53:15 -0800239 Callback failure_callback_;
240 Callback refresh_callback_;
Paul Stewart1f916e42013-12-23 09:52:54 -0800241 Callback expire_callback_;
Samuel Tan815a6fb2014-10-23 16:53:59 -0700242 struct timeval current_lease_expiration_time_;
Paul Stewart8ae18742015-06-16 13:13:10 -0700243 Time* time_;
Darin Petkove02b3ca2011-05-31 16:00:44 -0700244
Darin Petkove02b3ca2011-05-31 16:00:44 -0700245 DISALLOW_COPY_AND_ASSIGN(IPConfig);
246};
247
248} // namespace shill
249
Ben Chanc45688b2014-07-02 23:50:45 -0700250#endif // SHILL_IPCONFIG_H_