blob: be8530e5ac6d431a2c6e57c93dddcc81ec3f8059 [file] [log] [blame]
Darin Petkov33af05c2012-02-28 10:10:30 +01001// 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 Stewart65512e12012-03-26 18:01:08 -07007#include <algorithm>
8
Paul Stewart66815332012-04-09 18:09:36 -07009#include <base/string_util.h>
Darin Petkov33af05c2012-02-28 10:10:30 +010010#include <chromeos/dbus/service_constants.h>
11
12#include "shill/error.h"
Darin Petkov9d1bbe72012-04-25 10:58:59 +020013#include "shill/l2tp_ipsec_driver.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Paul Stewartca6abd42012-03-01 15:45:29 -080015#include "shill/manager.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010016#include "shill/openvpn_driver.h"
Paul Stewart66815332012-04-09 18:09:36 -070017#include "shill/profile.h"
18#include "shill/store_interface.h"
Darin Petkov33af05c2012-02-28 10:10:30 +010019#include "shill/vpn_service.h"
20
Paul Stewart66815332012-04-09 18:09:36 -070021using std::set;
Darin Petkov33af05c2012-02-28 10:10:30 +010022using std::string;
Paul Stewartca6abd42012-03-01 15:45:29 -080023using std::vector;
Darin Petkov33af05c2012-02-28 10:10:30 +010024
25namespace shill {
26
27VPNProvider::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
36VPNProvider::~VPNProvider() {}
37
38void VPNProvider::Start() {}
39
40void VPNProvider::Stop() {}
41
42VPNServiceRefPtr VPNProvider::GetService(const KeyValueStore &args,
43 Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070044 SLOG(VPN, 2) << __func__;
Darin Petkov7f060332012-03-14 11:46:47 +010045 string type = args.LookupString(flimflam::kProviderTypeProperty, "");
46 if (type.empty()) {
Darin Petkov33af05c2012-02-28 10:10:30 +010047 Error::PopulateAndLog(
48 error, Error::kNotSupported, "Missing VPN type property.");
49 return NULL;
50 }
Paul Stewartca6abd42012-03-01 15:45:29 -080051
Darin Petkov9c6e9812013-03-26 13:49:07 +010052 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 Petkov02867712012-03-12 14:25:05 +010059 string storage_id = VPNService::CreateStorageIdentifier(args, error);
60 if (storage_id.empty()) {
61 return NULL;
62 }
63
Darin Petkov9c6e9812013-03-26 13:49:07 +010064 string name = args.LookupString(flimflam::kProviderNameProperty, "");
65 if (name.empty()) {
66 name = args.LookupString(flimflam::kNameProperty, "");
Paul Stewart451aa7f2012-04-11 19:07:58 -070067 }
68
Darin Petkov9c6e9812013-03-26 13:49:07 +010069 // Find a service in the provider list which matches these parameters.
70 VPNServiceRefPtr service = FindService(type, name, host);
71 if (service == NULL) {
72 service = CreateService(type, name, storage_id, error);
73 }
Darin Petkov79d74c92012-03-07 17:20:32 +010074 return service;
Paul Stewartca6abd42012-03-01 15:45:29 -080075}
76
77bool VPNProvider::OnDeviceInfoAvailable(const string &link_name,
78 int interface_index) {
79 for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
80 it != services_.end();
81 ++it) {
82 if ((*it)->driver()->ClaimInterface(link_name, interface_index)) {
83 return true;
84 }
85 }
86
87 return false;
Darin Petkov33af05c2012-02-28 10:10:30 +010088}
89
Paul Stewart65512e12012-03-26 18:01:08 -070090void VPNProvider::RemoveService(VPNServiceRefPtr service) {
91 vector<VPNServiceRefPtr>::iterator it;
92 it = std::find(services_.begin(), services_.end(), service);
93 if (it != services_.end()) {
94 services_.erase(it);
95 }
96}
97
Paul Stewart66815332012-04-09 18:09:36 -070098void VPNProvider::CreateServicesFromProfile(ProfileRefPtr profile) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070099 SLOG(VPN, 2) << __func__;
Paul Stewart66815332012-04-09 18:09:36 -0700100 const StoreInterface *storage = profile->GetConstStorage();
101 set<string> groups =
102 storage->GetGroupsWithKey(flimflam::kProviderTypeProperty);
103 for (set<string>::iterator it = groups.begin(); it != groups.end(); ++it) {
104 if (!StartsWithASCII(*it, "vpn_", false)) {
105 continue;
106 }
107
108 string type;
109 if (!storage->GetString(*it, flimflam::kProviderTypeProperty, &type)) {
110 LOG(ERROR) << "Group " << *it << " is missing the "
111 << flimflam::kProviderTypeProperty << " property.";
112 continue;
113 }
114
Paul Stewart451aa7f2012-04-11 19:07:58 -0700115 string name;
116 if (!storage->GetString(*it, flimflam::kProviderNameProperty, &name) &&
117 !storage->GetString(*it, flimflam::kNameProperty, &name)) {
118 LOG(ERROR) << "Group " << *it << " is missing the "
119 << flimflam::kProviderNameProperty << " property.";
120 continue;
121 }
122
Darin Petkov9c6e9812013-03-26 13:49:07 +0100123 string host;
124 if (!storage->GetString(*it, flimflam::kProviderHostProperty, &host)) {
125 LOG(ERROR) << "Group " << *it << " is missing the "
126 << flimflam::kProviderHostProperty << " property.";
127 continue;
128 }
129
130 VPNServiceRefPtr service = FindService(type, name, host);
Paul Stewart66815332012-04-09 18:09:36 -0700131 if (service != NULL) {
132 // If the service already exists, it does not need to be configured,
133 // since PushProfile would have already called ConfigureService on it.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700134 SLOG(VPN, 2) << "Service already exists " << *it;
Paul Stewart66815332012-04-09 18:09:36 -0700135 continue;
136 }
137
Paul Stewart66815332012-04-09 18:09:36 -0700138 Error error;
Paul Stewart451aa7f2012-04-11 19:07:58 -0700139 service = CreateService(type, name, *it, &error);
Paul Stewart66815332012-04-09 18:09:36 -0700140
141 if (service == NULL) {
142 LOG(ERROR) << "Could not create service for " << *it;
143 continue;
144 }
145
146 if (!profile->ConfigureService(service)) {
147 LOG(ERROR) << "Could not configure service for " << *it;
148 continue;
149 }
150 }
151}
152
153VPNServiceRefPtr VPNProvider::CreateService(const string &type,
Paul Stewart451aa7f2012-04-11 19:07:58 -0700154 const string &name,
Paul Stewart66815332012-04-09 18:09:36 -0700155 const string &storage_id,
Paul Stewart66815332012-04-09 18:09:36 -0700156 Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700157 SLOG(VPN, 2) << __func__ << " type " << type << " name " << name
158 << " storage id " << storage_id;
Darin Petkovc3505a52013-03-18 15:13:29 +0100159#if defined(DISABLE_VPN)
160
161 Error::PopulateAndLog(error, Error::kNotSupported, "VPN is not supported.");
162 return NULL;
163
164#else
165
Paul Stewart66815332012-04-09 18:09:36 -0700166 scoped_ptr<VPNDriver> driver;
167 if (type == flimflam::kProviderOpenVpn) {
168 driver.reset(new OpenVPNDriver(
169 control_interface_, dispatcher_, metrics_, manager_,
Paul Stewart451aa7f2012-04-11 19:07:58 -0700170 manager_->device_info(), manager_->glib()));
Darin Petkov9d1bbe72012-04-25 10:58:59 +0200171 } else if (type == flimflam::kProviderL2tpIpsec) {
172 driver.reset(new L2TPIPSecDriver(
173 control_interface_, dispatcher_, metrics_, manager_,
174 manager_->device_info(), manager_->glib()));
Paul Stewart66815332012-04-09 18:09:36 -0700175 } else {
176 Error::PopulateAndLog(
177 error, Error::kNotSupported, "Unsupported VPN type: " + type);
178 return NULL;
179 }
180
181 VPNServiceRefPtr service = new VPNService(
182 control_interface_, dispatcher_, metrics_, manager_, driver.release());
183 service->set_storage_id(storage_id);
184 service->InitDriverPropertyStore();
Paul Stewart66815332012-04-09 18:09:36 -0700185 if (!name.empty()) {
186 service->set_friendly_name(name);
187 }
188 services_.push_back(service);
189 manager_->RegisterService(service);
190
191 return service;
Darin Petkovc3505a52013-03-18 15:13:29 +0100192
193#endif // DISABLE_VPN
Paul Stewart66815332012-04-09 18:09:36 -0700194}
195
Darin Petkov9c6e9812013-03-26 13:49:07 +0100196VPNServiceRefPtr VPNProvider::FindService(const string &type,
197 const string &name,
198 const string &host) {
Paul Stewart66815332012-04-09 18:09:36 -0700199 for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
200 it != services_.end();
201 ++it) {
202 if (type == (*it)->driver()->GetProviderType() &&
Darin Petkov9c6e9812013-03-26 13:49:07 +0100203 name == (*it)->friendly_name() &&
204 host == (*it)->driver()->GetHost()) {
Paul Stewart66815332012-04-09 18:09:36 -0700205 return *it;
206 }
207 }
Paul Stewart66815332012-04-09 18:09:36 -0700208 return NULL;
209}
210
Darin Petkov4cbff5b2013-01-29 16:29:05 +0100211bool VPNProvider::HasActiveService() const {
212 for (vector<VPNServiceRefPtr>::const_iterator it = services_.begin();
213 it != services_.end(); ++it) {
214 if ((*it)->IsConnecting() || (*it)->IsConnected()) {
215 return true;
216 }
217 }
218 return false;
219}
220
Darin Petkov33af05c2012-02-28 10:10:30 +0100221} // namespace shill