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 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 9 | #include <base/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 | |
| 42 | VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args, |
| 43 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 44 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 45 | string type = args.LookupString(flimflam::kProviderTypeProperty, ""); |
| 46 | if (type.empty()) { |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 47 | Error::PopulateAndLog( |
| 48 | error, Error::kNotSupported, "Missing VPN type property."); |
| 49 | return NULL; |
| 50 | } |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 51 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 52 | string host = args.LookupString(flimflam::kProviderHostProperty, ""); |
| 53 | if (host.empty()) { |
| 54 | Error::PopulateAndLog( |
| 55 | error, Error::kNotSupported, "Missing VPN host property."); |
| 56 | return NULL; |
| 57 | } |
| 58 | |
Darin Petkov | 0286771 | 2012-03-12 14:25:05 +0100 | [diff] [blame] | 59 | string storage_id = VPNService::CreateStorageIdentifier(args, error); |
| 60 | if (storage_id.empty()) { |
| 61 | return NULL; |
| 62 | } |
| 63 | |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 64 | string name = args.LookupString(flimflam::kNameProperty, ""); |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 65 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 66 | // Find a service in the provider list which matches these parameters. |
| 67 | VPNServiceRefPtr service = FindService(type, name, host); |
| 68 | if (service == NULL) { |
| 69 | service = CreateService(type, name, storage_id, error); |
| 70 | } |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 71 | return service; |
Paul Stewart | ca6abd4 | 2012-03-01 15:45:29 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool VPNProvider::OnDeviceInfoAvailable(const string &link_name, |
| 75 | int interface_index) { |
| 76 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 77 | it != services_.end(); |
| 78 | ++it) { |
| 79 | if ((*it)->driver()->ClaimInterface(link_name, interface_index)) { |
| 80 | return true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return false; |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 87 | void VPNProvider::RemoveService(VPNServiceRefPtr service) { |
| 88 | vector<VPNServiceRefPtr>::iterator it; |
| 89 | it = std::find(services_.begin(), services_.end(), service); |
| 90 | if (it != services_.end()) { |
| 91 | services_.erase(it); |
| 92 | } |
| 93 | } |
| 94 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 95 | void VPNProvider::CreateServicesFromProfile(ProfileRefPtr profile) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 96 | SLOG(VPN, 2) << __func__; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 97 | const StoreInterface *storage = profile->GetConstStorage(); |
| 98 | set<string> groups = |
| 99 | storage->GetGroupsWithKey(flimflam::kProviderTypeProperty); |
| 100 | for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) { |
| 101 | if (!StartsWithASCII(*it, "vpn_", false)) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | string type; |
| 106 | if (!storage->GetString(*it, flimflam::kProviderTypeProperty, &type)) { |
| 107 | LOG(ERROR) << "Group " << *it << " is missing the " |
| 108 | << flimflam::kProviderTypeProperty << " property."; |
| 109 | continue; |
| 110 | } |
| 111 | |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 112 | string name; |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 113 | if (!storage->GetString(*it, flimflam::kNameProperty, &name)) { |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 114 | LOG(ERROR) << "Group " << *it << " is missing the " |
Darin Petkov | 4e02ba2 | 2013-04-02 13:44:08 +0200 | [diff] [blame] | 115 | << flimflam::kNameProperty << " property."; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 116 | continue; |
| 117 | } |
| 118 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 119 | string host; |
| 120 | if (!storage->GetString(*it, flimflam::kProviderHostProperty, &host)) { |
| 121 | LOG(ERROR) << "Group " << *it << " is missing the " |
| 122 | << flimflam::kProviderHostProperty << " property."; |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | VPNServiceRefPtr service = FindService(type, name, host); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 127 | if (service != NULL) { |
| 128 | // If the service already exists, it does not need to be configured, |
| 129 | // since PushProfile would have already called ConfigureService on it. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 130 | SLOG(VPN, 2) << "Service already exists " << *it; |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 134 | Error error; |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 135 | service = CreateService(type, name, *it, &error); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 136 | |
| 137 | if (service == NULL) { |
| 138 | LOG(ERROR) << "Could not create service for " << *it; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | if (!profile->ConfigureService(service)) { |
| 143 | LOG(ERROR) << "Could not configure service for " << *it; |
| 144 | continue; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | VPNServiceRefPtr VPNProvider::CreateService(const string &type, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 150 | const string &name, |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 151 | const string &storage_id, |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 152 | Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 153 | SLOG(VPN, 2) << __func__ << " type " << type << " name " << name |
| 154 | << " storage id " << storage_id; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 155 | #if defined(DISABLE_VPN) |
| 156 | |
| 157 | Error::PopulateAndLog(error, Error::kNotSupported, "VPN is not supported."); |
| 158 | return NULL; |
| 159 | |
| 160 | #else |
| 161 | |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 162 | scoped_ptr<VPNDriver> driver; |
| 163 | if (type == flimflam::kProviderOpenVpn) { |
| 164 | driver.reset(new OpenVPNDriver( |
| 165 | control_interface_, dispatcher_, metrics_, manager_, |
Paul Stewart | 451aa7f | 2012-04-11 19:07:58 -0700 | [diff] [blame] | 166 | manager_->device_info(), manager_->glib())); |
Darin Petkov | 9d1bbe7 | 2012-04-25 10:58:59 +0200 | [diff] [blame] | 167 | } else if (type == flimflam::kProviderL2tpIpsec) { |
| 168 | driver.reset(new L2TPIPSecDriver( |
| 169 | control_interface_, dispatcher_, metrics_, manager_, |
| 170 | manager_->device_info(), manager_->glib())); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 171 | } else { |
| 172 | Error::PopulateAndLog( |
| 173 | error, Error::kNotSupported, "Unsupported VPN type: " + type); |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | VPNServiceRefPtr service = new VPNService( |
| 178 | control_interface_, dispatcher_, metrics_, manager_, driver.release()); |
| 179 | service->set_storage_id(storage_id); |
| 180 | service->InitDriverPropertyStore(); |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 181 | if (!name.empty()) { |
| 182 | service->set_friendly_name(name); |
| 183 | } |
| 184 | services_.push_back(service); |
| 185 | manager_->RegisterService(service); |
| 186 | |
| 187 | return service; |
Darin Petkov | c3505a5 | 2013-03-18 15:13:29 +0100 | [diff] [blame] | 188 | |
| 189 | #endif // DISABLE_VPN |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 192 | VPNServiceRefPtr VPNProvider::FindService(const string &type, |
| 193 | const string &name, |
| 194 | const string &host) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 195 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 196 | it != services_.end(); |
| 197 | ++it) { |
| 198 | if (type == (*it)->driver()->GetProviderType() && |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 199 | name == (*it)->friendly_name() && |
| 200 | host == (*it)->driver()->GetHost()) { |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 201 | return *it; |
| 202 | } |
| 203 | } |
Paul Stewart | 6681533 | 2012-04-09 18:09:36 -0700 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
| 206 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 207 | bool VPNProvider::HasActiveService() const { |
| 208 | for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin(); |
| 209 | it != services_.end(); ++it) { |
| 210 | if ((*it)->IsConnecting() || (*it)->IsConnected()) { |
| 211 | return true; |
| 212 | } |
| 213 | } |
| 214 | return false; |
| 215 | } |
| 216 | |
Darin Petkov | 33af05c | 2012-02-28 10:10:30 +0100 | [diff] [blame] | 217 | } // namespace shill |