Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 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/vpn_provider.h" |
| 6 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 9 | #include <base/strings/string_util.h> |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | |
| 12 | #include "shill/error.h" |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 13 | #include "shill/l2tp_ipsec_driver.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 14 | #include "shill/logging.h" |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 15 | #include "shill/manager.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 16 | #include "shill/openvpn_driver.h" |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 17 | #include "shill/profile.h" |
| 18 | #include "shill/store_interface.h" |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 19 | #include "shill/vpn_service.h" |
| 20 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 21 | using std::set; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 22 | using std::string; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 23 | using std::vector; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 24 | |
| 25 | namespace shill { |
| 26 | |
| 27 | VPNProvider::VPNProvider(ControlInterface *control_interface, |
| 28 | EventDispatcher *dispatcher, |
| 29 | Metrics *metrics, |
| 30 | Manager *manager) |
| 31 | : control_interface_(control_interface), |
| 32 | dispatcher_(dispatcher), |
| 33 | metrics_(metrics), |
| 34 | manager_(manager) {} |
| 35 | |
| 36 | VPNProvider::~VPNProvider() {} |
| 37 | |
| 38 | void VPNProvider::Start() {} |
| 39 | |
| 40 | void VPNProvider::Stop() {} |
| 41 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 42 | // static |
| 43 | bool VPNProvider::GetServiceParametersFromArgs(const KeyValueStore &args, |
| 44 | string *type_ptr, |
| 45 | string *name_ptr, |
| 46 | string *host_ptr, |
| 47 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 48 | SLOG(VPN, 2) << __func__; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 49 | string type = args.LookupString(kProviderTypeProperty, ""); |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 50 | if (type.empty()) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 51 | Error::PopulateAndLog( |
| 52 | error, Error::kNotSupported, "Missing VPN type property."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 53 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 54 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 55 | |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 56 | string host = args.LookupString(kProviderHostProperty, ""); |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 57 | if (host.empty()) { |
| 58 | Error::PopulateAndLog( |
| 59 | error, Error::kNotSupported, "Missing VPN host property."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | *type_ptr = type, |
| 64 | *host_ptr = host, |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 65 | *name_ptr = args.LookupString(kNameProperty, ""); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | ServiceRefPtr VPNProvider::GetService(const KeyValueStore &args, |
| 71 | Error *error) { |
| 72 | SLOG(VPN, 2) << __func__; |
| 73 | string type; |
| 74 | string name; |
| 75 | string host; |
| 76 | |
| 77 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 81 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 82 | if (storage_id.empty()) { |
| 83 | return NULL; |
| 84 | } |
| 85 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 86 | // Find a service in the provider list which matches these parameters. |
| 87 | VPNServiceRefPtr service = FindService(type, name, host); |
| 88 | if (service == NULL) { |
| 89 | service = CreateService(type, name, storage_id, error); |
| 90 | } |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 91 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 94 | ServiceRefPtr VPNProvider::FindSimilarService(const KeyValueStore &args, |
| 95 | Error *error) const { |
| 96 | SLOG(VPN, 2) << __func__; |
| 97 | string type; |
| 98 | string name; |
| 99 | string host; |
| 100 | |
| 101 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | // Find a service in the provider list which matches these parameters. |
| 106 | VPNServiceRefPtr service = FindService(type, name, host); |
| 107 | if (!service) { |
| 108 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 109 | } |
| 110 | |
| 111 | return service; |
| 112 | } |
| 113 | |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 114 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 115 | int interface_index) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 116 | for (const auto &service : services_) { |
| 117 | if (service->driver()->ClaimInterface(link_name, interface_index)) { |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 125 | void VPNProvider::RemoveService(VPNServiceRefPtr service) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 126 | const auto it = std::find(services_.begin(), services_.end(), service); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 127 | if (it != services_.end()) { |
| 128 | services_.erase(it); |
| 129 | } |
| 130 | } |
| 131 | |
Paul Stewart | 0e51ad9 | 2013-07-26 14:42:55 -0700 | [diff] [blame] | 132 | void VPNProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 133 | SLOG(VPN, 2) << __func__; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 134 | const StoreInterface *storage = profile->GetConstStorage(); |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 135 | for (const auto &group : storage->GetGroupsWithKey(kProviderTypeProperty)) { |
| 136 | if (!StartsWithASCII(group, "vpn_", false)) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 137 | continue; |
| 138 | } |
| 139 | |
| 140 | string type; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 141 | if (!storage->GetString(group, kProviderTypeProperty, &type)) { |
| 142 | LOG(ERROR) << "Group " << group << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 143 | << kProviderTypeProperty << " property."; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 147 | string name; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 148 | if (!storage->GetString(group, kNameProperty, &name)) { |
| 149 | LOG(ERROR) << "Group " << group << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 150 | << kNameProperty << " property."; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 154 | string host; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 155 | if (!storage->GetString(group, kProviderHostProperty, &host)) { |
| 156 | LOG(ERROR) << "Group " << group << " is missing the " |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 157 | << kProviderHostProperty << " property."; |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 158 | continue; |
| 159 | } |
| 160 | |
| 161 | VPNServiceRefPtr service = FindService(type, name, host); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 162 | if (service != NULL) { |
| 163 | // If the service already exists, it does not need to be configured, |
| 164 | // since PushProfile would have already called ConfigureService on it. |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 165 | SLOG(VPN, 2) << "Service already exists " << group; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 166 | continue; |
| 167 | } |
| 168 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 169 | Error error; |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 170 | service = CreateService(type, name, group, &error); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 171 | |
| 172 | if (service == NULL) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 173 | LOG(ERROR) << "Could not create service for " << group; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
| 177 | if (!profile->ConfigureService(service)) { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 178 | LOG(ERROR) << "Could not configure service for " << group; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 184 | VPNServiceRefPtr VPNProvider::CreateServiceInner(const string &type, |
| 185 | const string &name, |
| 186 | const string &storage_id, |
| 187 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 188 | SLOG(VPN, 2) << __func__ << " type " << type << " name " << name |
| 189 | << " storage id " << storage_id; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 190 | #if defined(DISABLE_VPN) |
| 191 | |
| 192 | Error::PopulateAndLog(error, Error::kNotSupported, "VPN is not supported."); |
| 193 | return NULL; |
| 194 | |
| 195 | #else |
| 196 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 197 | scoped_ptr<VPNDriver> driver; |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 198 | if (type == kProviderOpenVpn) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 199 | driver.reset(new OpenVPNDriver( |
| 200 | control_interface_, dispatcher_, metrics_, manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 201 | manager_->device_info(), manager_->glib())); |
Ben Chan | 7372878 | 2013-09-20 13:40:54 -0700 | [diff] [blame] | 202 | } else if (type == kProviderL2tpIpsec) { |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 203 | driver.reset(new L2TPIPSecDriver( |
| 204 | control_interface_, dispatcher_, metrics_, manager_, |
| 205 | manager_->device_info(), manager_->glib())); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 206 | } else { |
| 207 | Error::PopulateAndLog( |
| 208 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | VPNServiceRefPtr service = new VPNService( |
| 213 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
| 214 | service->set_storage_id(storage_id); |
| 215 | service->InitDriverPropertyStore(); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 216 | if (!name.empty()) { |
| 217 | service->set_friendly_name(name); |
| 218 | } |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 219 | return service; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 220 | |
| 221 | #endif // DISABLE_VPN |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 224 | VPNServiceRefPtr VPNProvider::CreateService(const string &type, |
| 225 | const string &name, |
| 226 | const string &storage_id, |
| 227 | Error *error) { |
| 228 | VPNServiceRefPtr service = CreateServiceInner(type, name, storage_id, error); |
| 229 | if (service) { |
| 230 | services_.push_back(service); |
| 231 | manager_->RegisterService(service); |
| 232 | } |
| 233 | |
| 234 | return service; |
| 235 | } |
| 236 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 237 | VPNServiceRefPtr VPNProvider::FindService(const string &type, |
| 238 | const string &name, |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 239 | const string &host) const { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 240 | for (const auto &service : services_) { |
| 241 | if (type == service->driver()->GetProviderType() && |
| 242 | name == service->friendly_name() && |
| 243 | host == service->driver()->GetHost()) { |
| 244 | return service; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 247 | return NULL; |
| 248 | } |
| 249 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 250 | ServiceRefPtr VPNProvider::CreateTemporaryService( |
| 251 | const KeyValueStore &args, Error *error) { |
| 252 | string type; |
| 253 | string name; |
| 254 | string host; |
| 255 | |
| 256 | if (!GetServiceParametersFromArgs(args, &type, &name, &host, error)) { |
| 257 | return NULL; |
| 258 | } |
| 259 | |
| 260 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 261 | if (storage_id.empty()) { |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | return CreateServiceInner(type, name, storage_id, error); |
| 266 | } |
| 267 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 268 | bool VPNProvider::HasActiveService() const { |
Paul Stewart | 6db7b24 | 2014-05-02 15:34:21 -0700 | [diff] [blame] | 269 | for (const auto &service : services_) { |
| 270 | if (service->IsConnecting() || service->IsConnected()) { |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 277 | } // namespace shill |