blob: 73ed26b5132b007426016cab14f7633a0f9df0b2 [file] [log] [blame]
Darin Petkov7476a262012-04-12 16:30:46 +02001// 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
Paul Stewart5bbf93b2013-03-21 12:06:08 -07005// The term "L2TP / IPSec" refers to a pair of layered protocols used
6// together to establish a tunneled VPN connection. First, an "IPSec"
7// link is created, which secures a single IP traffic pair between the
8// client and server. For this link to complete, one or two levels of
9// authentication are performed. The first, inner mandatory authentication
10// ensures the two parties establishing the IPSec link are correct. This
11// can use a certificate exchange or a less secure "shared group key"
12// (PSK) authentication. An optional outer IPSec authentication can also be
13// performed, which is not fully supported by shill's implementation.
14// In order to support "tunnel groups" from some vendor VPNs shill supports
15// supplying the authentication realm portion during the outer authentication.
16// Notably, XAUTH and other forms of user authentication on this outer link
17// are not supported.
18//
19// When IPSec authentication completes, traffic is tunneled through a
20// layer 2 tunnel, called "L2TP". Using the secured link, we tunnel a
21// PPP link, through which a second layer of authentication is performed,
22// using the provided "user" and "password" properties.
23
Darin Petkov7476a262012-04-12 16:30:46 +020024#include "shill/l2tp_ipsec_driver.h"
25
Darin Petkov85d53172013-03-13 16:43:28 +010026#include <sys/wait.h>
27
Darin Petkov69990222012-11-14 09:25:25 +010028#include <base/bind.h>
Darin Petkovf7ef50a2012-04-16 20:54:31 +020029#include <base/file_util.h>
Darin Petkov209e6292012-04-20 11:33:32 +020030#include <base/string_util.h>
Darin Petkov7476a262012-04-12 16:30:46 +020031#include <chromeos/dbus/service_constants.h>
Darin Petkov85d53172013-03-13 16:43:28 +010032#include <chromeos/vpn-manager/service_error.h>
Darin Petkov7476a262012-04-12 16:30:46 +020033
Paul Stewart5baebb72013-03-14 11:43:29 -070034#include "shill/certificate_file.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020035#include "shill/device_info.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020036#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070037#include "shill/logging.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020038#include "shill/manager.h"
39#include "shill/nss.h"
Darin Petkov5a850472012-06-06 15:44:24 +020040#include "shill/process_killer.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020041#include "shill/vpn.h"
Darin Petkov209e6292012-04-20 11:33:32 +020042#include "shill/vpn_service.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020043
Darin Petkov69990222012-11-14 09:25:25 +010044using base::Bind;
Darin Petkov5a850472012-06-06 15:44:24 +020045using base::Closure;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -080046using base::FilePath;
Darin Petkov209e6292012-04-20 11:33:32 +020047using std::map;
Darin Petkov7476a262012-04-12 16:30:46 +020048using std::string;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020049using std::vector;
Darin Petkov7476a262012-04-12 16:30:46 +020050
51namespace shill {
52
Darin Petkovf7ef50a2012-04-16 20:54:31 +020053namespace {
54const char kL2TPIPSecIPSecTimeoutProperty[] = "L2TPIPsec.IPsecTimeout";
55const char kL2TPIPSecLeftProtoPortProperty[] = "L2TPIPsec.LeftProtoPort";
56const char kL2TPIPSecLengthBitProperty[] = "L2TPIPsec.LengthBit";
57const char kL2TPIPSecPFSProperty[] = "L2TPIPsec.PFS";
58const char kL2TPIPSecRefusePapProperty[] = "L2TPIPsec.RefusePap";
59const char kL2TPIPSecRekeyProperty[] = "L2TPIPsec.Rekey";
60const char kL2TPIPSecRequireAuthProperty[] = "L2TPIPsec.RequireAuth";
61const char kL2TPIPSecRequireChapProperty[] = "L2TPIPsec.RequireChap";
62const char kL2TPIPSecRightProtoPortProperty[] = "L2TPIPsec.RightProtoPort";
63} // namespace
64
65// static
Darin Petkov95f317f2012-10-22 13:37:43 +020066const char L2TPIPSecDriver::kPPPDPlugin[] = SHIMDIR "/shill-pppd-plugin.so";
Darin Petkovd4325392012-04-23 15:48:22 +020067// static
Darin Petkov209e6292012-04-20 11:33:32 +020068const char L2TPIPSecDriver::kL2TPIPSecVPNPath[] = "/usr/sbin/l2tpipsec_vpn";
Darin Petkovd4325392012-04-23 15:48:22 +020069// static
70const VPNDriver::Property L2TPIPSecDriver::kProperties[] = {
Darin Petkova56bf972012-10-09 12:27:32 +020071 { flimflam::kL2tpIpsecAuthenticationType, 0 },
Darin Petkovd4325392012-04-23 15:48:22 +020072 { flimflam::kL2tpIpsecCaCertNssProperty, 0 },
73 { flimflam::kL2tpIpsecClientCertIdProperty, 0 },
74 { flimflam::kL2tpIpsecClientCertSlotProperty, 0 },
Darin Petkova56bf972012-10-09 12:27:32 +020075 { flimflam::kL2tpIpsecIkeVersion, 0 },
Darin Petkov02236552012-06-11 13:15:19 +020076 { flimflam::kL2tpIpsecPasswordProperty,
77 Property::kCredential | Property::kWriteOnly },
Darin Petkovcb715292012-04-25 13:04:37 +020078 { flimflam::kL2tpIpsecPinProperty, Property::kCredential },
79 { flimflam::kL2tpIpsecPskProperty, Property::kCredential },
Darin Petkovd4325392012-04-23 15:48:22 +020080 { flimflam::kL2tpIpsecUserProperty, 0 },
Darin Petkov2c773c22012-04-26 12:54:11 +020081 { flimflam::kProviderHostProperty, 0 },
82 { flimflam::kProviderNameProperty, 0 },
83 { flimflam::kProviderTypeProperty, 0 },
Paul Stewart5baebb72013-03-14 11:43:29 -070084 { kL2tpIpsecCaCertPemProperty, 0 },
Paul Stewart5bbf93b2013-03-21 12:06:08 -070085 { kL2tpIpsecTunnelGroupProperty, 0 },
Darin Petkovd4325392012-04-23 15:48:22 +020086 { kL2TPIPSecIPSecTimeoutProperty, 0 },
87 { kL2TPIPSecLeftProtoPortProperty, 0 },
88 { kL2TPIPSecLengthBitProperty, 0 },
89 { kL2TPIPSecPFSProperty, 0 },
90 { kL2TPIPSecRefusePapProperty, 0 },
91 { kL2TPIPSecRekeyProperty, 0 },
92 { kL2TPIPSecRequireAuthProperty, 0 },
93 { kL2TPIPSecRequireChapProperty, 0 },
94 { kL2TPIPSecRightProtoPortProperty, 0 },
95};
Darin Petkovf7ef50a2012-04-16 20:54:31 +020096
Darin Petkov209e6292012-04-20 11:33:32 +020097L2TPIPSecDriver::L2TPIPSecDriver(ControlInterface *control,
Darin Petkovf8046b82012-04-24 16:29:23 +020098 EventDispatcher *dispatcher,
99 Metrics *metrics,
Darin Petkov209e6292012-04-20 11:33:32 +0200100 Manager *manager,
Darin Petkovf8046b82012-04-24 16:29:23 +0200101 DeviceInfo *device_info,
Darin Petkov209e6292012-04-20 11:33:32 +0200102 GLib *glib)
Darin Petkov602303f2012-06-06 12:15:59 +0200103 : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)),
Darin Petkovb451d6e2012-04-23 11:56:41 +0200104 control_(control),
Darin Petkovf8046b82012-04-24 16:29:23 +0200105 metrics_(metrics),
106 device_info_(device_info),
Darin Petkov209e6292012-04-20 11:33:32 +0200107 glib_(glib),
108 nss_(NSS::GetInstance()),
Darin Petkov5a850472012-06-06 15:44:24 +0200109 process_killer_(ProcessKiller::GetInstance()),
Paul Stewart5baebb72013-03-14 11:43:29 -0700110 certificate_file_(new CertificateFile(glib)),
Darin Petkov209e6292012-04-20 11:33:32 +0200111 pid_(0),
112 child_watch_tag_(0) {}
Darin Petkov7476a262012-04-12 16:30:46 +0200113
Darin Petkov209e6292012-04-20 11:33:32 +0200114L2TPIPSecDriver::~L2TPIPSecDriver() {
Darin Petkov85d53172013-03-13 16:43:28 +0100115 IdleService();
Darin Petkov209e6292012-04-20 11:33:32 +0200116}
Darin Petkov7476a262012-04-12 16:30:46 +0200117
118bool L2TPIPSecDriver::ClaimInterface(const string &link_name,
119 int interface_index) {
Darin Petkovf8f970a2012-09-03 11:32:55 +0200120 // TODO(petkov): crosbug.com/29970.
Darin Petkov7476a262012-04-12 16:30:46 +0200121 NOTIMPLEMENTED();
122 return false;
123}
124
125void L2TPIPSecDriver::Connect(const VPNServiceRefPtr &service, Error *error) {
Darin Petkov0cd0d1e2013-02-11 12:49:10 +0100126 StartConnectTimeout(kDefaultConnectTimeoutSeconds);
Darin Petkov209e6292012-04-20 11:33:32 +0200127 service_ = service;
128 service_->SetState(Service::kStateConfiguring);
129 rpc_task_.reset(new RPCTask(control_, this));
130 if (!SpawnL2TPIPSecVPN(error)) {
Darin Petkov1c049c72013-03-21 13:15:45 +0100131 FailService(Service::kFailureInternal);
Darin Petkov209e6292012-04-20 11:33:32 +0200132 }
Darin Petkov7476a262012-04-12 16:30:46 +0200133}
134
135void L2TPIPSecDriver::Disconnect() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200136 SLOG(VPN, 2) << __func__;
Darin Petkov85d53172013-03-13 16:43:28 +0100137 IdleService();
Darin Petkov7476a262012-04-12 16:30:46 +0200138}
139
Darin Petkov5eb05422012-05-11 15:45:25 +0200140void L2TPIPSecDriver::OnConnectionDisconnected() {
Darin Petkova42afe32013-02-05 16:53:52 +0100141 LOG(INFO) << "Underlying connection disconnected.";
Darin Petkov85d53172013-03-13 16:43:28 +0100142 IdleService();
Darin Petkova42afe32013-02-05 16:53:52 +0100143}
144
145void L2TPIPSecDriver::OnConnectTimeout() {
146 VPNDriver::OnConnectTimeout();
Darin Petkov85d53172013-03-13 16:43:28 +0100147 FailService(Service::kFailureConnect);
Darin Petkov5eb05422012-05-11 15:45:25 +0200148}
149
Darin Petkov7476a262012-04-12 16:30:46 +0200150string L2TPIPSecDriver::GetProviderType() const {
151 return flimflam::kProviderL2tpIpsec;
152}
153
Darin Petkov85d53172013-03-13 16:43:28 +0100154void L2TPIPSecDriver::IdleService() {
155 Cleanup(Service::kStateIdle, Service::kFailureUnknown);
156}
157
158void L2TPIPSecDriver::FailService(Service::ConnectFailure failure) {
159 Cleanup(Service::kStateFailure, failure);
160}
161
162void L2TPIPSecDriver::Cleanup(Service::ConnectState state,
163 Service::ConnectFailure failure) {
164 SLOG(VPN, 2) << __func__ << "("
165 << Service::ConnectStateToString(state) << ", "
166 << Service::ConnectFailureToString(failure) << ")";
Darin Petkov602303f2012-06-06 12:15:59 +0200167 StopConnectTimeout();
Darin Petkov0e9735d2012-04-24 12:33:45 +0200168 DeletePSKFile();
Darin Petkov209e6292012-04-20 11:33:32 +0200169 if (child_watch_tag_) {
170 glib_->SourceRemove(child_watch_tag_);
171 child_watch_tag_ = 0;
Darin Petkov209e6292012-04-20 11:33:32 +0200172 }
173 if (pid_) {
Darin Petkov5a850472012-06-06 15:44:24 +0200174 process_killer_->Kill(pid_, Closure());
Darin Petkov209e6292012-04-20 11:33:32 +0200175 pid_ = 0;
176 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200177 if (device_) {
178 device_->OnDisconnected();
179 device_->SetEnabled(false);
180 device_ = NULL;
181 }
Darin Petkov209e6292012-04-20 11:33:32 +0200182 rpc_task_.reset();
183 if (service_) {
Darin Petkov85d53172013-03-13 16:43:28 +0100184 if (state == Service::kStateFailure) {
185 service_->SetFailure(failure);
186 } else {
187 service_->SetState(state);
188 }
Darin Petkov209e6292012-04-20 11:33:32 +0200189 service_ = NULL;
190 }
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200191}
192
Darin Petkov0e9735d2012-04-24 12:33:45 +0200193void L2TPIPSecDriver::DeletePSKFile() {
194 if (!psk_file_.empty()) {
195 file_util::Delete(psk_file_, false);
196 psk_file_.clear();
197 }
198}
199
Darin Petkov209e6292012-04-20 11:33:32 +0200200bool L2TPIPSecDriver::SpawnL2TPIPSecVPN(Error *error) {
201 SLOG(VPN, 2) << __func__;
202
203 vector<string> options;
204 if (!InitOptions(&options, error)) {
205 return false;
206 }
Darin Petkov85d53172013-03-13 16:43:28 +0100207 LOG(INFO) << "L2TP/IPSec VPN process options: " << JoinString(options, ' ');
Darin Petkov209e6292012-04-20 11:33:32 +0200208
209 // TODO(petkov): This code needs to be abstracted away in a separate external
210 // process module (crosbug.com/27131).
211 vector<char *> process_args;
212 process_args.push_back(const_cast<char *>(kL2TPIPSecVPNPath));
213 for (vector<string>::const_iterator it = options.begin();
214 it != options.end(); ++it) {
215 process_args.push_back(const_cast<char *>(it->c_str()));
216 }
217 process_args.push_back(NULL);
218
219 vector<string> environment;
220 InitEnvironment(&environment);
221
222 vector<char *> process_env;
223 for (vector<string>::const_iterator it = environment.begin();
224 it != environment.end(); ++it) {
225 process_env.push_back(const_cast<char *>(it->c_str()));
226 }
227 process_env.push_back(NULL);
228
229 CHECK(!pid_);
Darin Petkov68710d72013-02-13 14:22:56 +0100230 if (!glib_->SpawnAsync(NULL,
231 process_args.data(),
232 process_env.data(),
233 G_SPAWN_DO_NOT_REAP_CHILD,
234 NULL,
235 NULL,
236 &pid_,
237 NULL)) {
Darin Petkov209e6292012-04-20 11:33:32 +0200238 Error::PopulateAndLog(error, Error::kInternalError,
239 string("Unable to spawn: ") + process_args[0]);
240 return false;
241 }
242 CHECK(!child_watch_tag_);
243 child_watch_tag_ = glib_->ChildWatchAdd(pid_, OnL2TPIPSecVPNDied, this);
244 return true;
245}
246
247void L2TPIPSecDriver::InitEnvironment(vector<string> *environment) {
Darin Petkov95f317f2012-10-22 13:37:43 +0200248 environment->push_back(string(kRPCTaskServiceVariable) + "=" +
249 rpc_task_->GetRpcConnectionIdentifier());
250 environment->push_back(string(kRPCTaskPathVariable) + "=" +
251 rpc_task_->GetRpcIdentifier());
Darin Petkov209e6292012-04-20 11:33:32 +0200252}
253
254bool L2TPIPSecDriver::InitOptions(vector<string> *options, Error *error) {
Darin Petkov01c66042012-04-26 11:10:45 +0200255 string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200256 if (vpnhost.empty()) {
257 Error::PopulateAndLog(
258 error, Error::kInvalidArguments, "VPN host not specified.");
Darin Petkov209e6292012-04-20 11:33:32 +0200259 return false;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200260 }
261
262 if (!InitPSKOptions(options, error)) {
Darin Petkov209e6292012-04-20 11:33:32 +0200263 return false;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200264 }
265
266 options->push_back("--remote_host");
267 options->push_back(vpnhost);
268 options->push_back("--pppd_plugin");
269 options->push_back(kPPPDPlugin);
270 // Disable pppd from configuring IP addresses, routes, DNS.
271 options->push_back("--nosystemconfig");
272
Paul Stewart5baebb72013-03-14 11:43:29 -0700273 // Accept a PEM CA certificate or an NSS certificate, but not both.
274 // Prefer PEM to NSS.
275 if (!InitPEMOptions(options)) {
276 InitNSSOptions(options);
277 }
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200278
279 AppendValueOption(flimflam::kL2tpIpsecClientCertIdProperty,
280 "--client_cert_id", options);
281 AppendValueOption(flimflam::kL2tpIpsecClientCertSlotProperty,
282 "--client_cert_slot", options);
283 AppendValueOption(flimflam::kL2tpIpsecPinProperty, "--user_pin", options);
284 AppendValueOption(flimflam::kL2tpIpsecUserProperty, "--user", options);
285 AppendValueOption(kL2TPIPSecIPSecTimeoutProperty, "--ipsec_timeout", options);
286 AppendValueOption(kL2TPIPSecLeftProtoPortProperty,
287 "--leftprotoport", options);
288 AppendFlag(kL2TPIPSecPFSProperty, "--pfs", "--nopfs", options);
289 AppendFlag(kL2TPIPSecRekeyProperty, "--rekey", "--norekey", options);
290 AppendValueOption(kL2TPIPSecRightProtoPortProperty,
291 "--rightprotoport", options);
292 AppendFlag(kL2TPIPSecRequireChapProperty,
293 "--require_chap", "--norequire_chap", options);
294 AppendFlag(kL2TPIPSecRefusePapProperty,
295 "--refuse_pap", "--norefuse_pap", options);
296 AppendFlag(kL2TPIPSecRequireAuthProperty,
297 "--require_authentication", "--norequire_authentication", options);
298 AppendFlag(kL2TPIPSecLengthBitProperty,
299 "--length_bit", "--nolength_bit", options);
Paul Stewart5bbf93b2013-03-21 12:06:08 -0700300 AppendValueOption(kL2tpIpsecTunnelGroupProperty, "--tunnel_group", options);
Darin Petkov209e6292012-04-20 11:33:32 +0200301 if (SLOG_IS_ON(VPN, 0)) {
302 options->push_back("--debug");
303 }
304 return true;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200305}
306
307bool L2TPIPSecDriver::InitPSKOptions(vector<string> *options, Error *error) {
Darin Petkov01c66042012-04-26 11:10:45 +0200308 string psk = args()->LookupString(flimflam::kL2tpIpsecPskProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200309 if (!psk.empty()) {
310 if (!file_util::CreateTemporaryFileInDir(
Darin Petkov0e9735d2012-04-24 12:33:45 +0200311 manager()->run_path(), &psk_file_) ||
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200312 chmod(psk_file_.value().c_str(), S_IRUSR | S_IWUSR) ||
313 file_util::WriteFile(psk_file_, psk.data(), psk.size()) !=
314 static_cast<int>(psk.size())) {
315 Error::PopulateAndLog(
316 error, Error::kInternalError, "Unable to setup psk file.");
317 return false;
318 }
319 options->push_back("--psk_file");
320 options->push_back(psk_file_.value());
321 }
322 return true;
323}
324
325void L2TPIPSecDriver::InitNSSOptions(vector<string> *options) {
326 string ca_cert =
Darin Petkov01c66042012-04-26 11:10:45 +0200327 args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200328 if (!ca_cert.empty()) {
Darin Petkov01c66042012-04-26 11:10:45 +0200329 const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200330 vector<char> id(vpnhost.begin(), vpnhost.end());
331 FilePath certfile = nss_->GetDERCertfile(ca_cert, id);
332 if (certfile.empty()) {
333 LOG(ERROR) << "Unable to extract certificate: " << ca_cert;
334 } else {
335 options->push_back("--server_ca_file");
336 options->push_back(certfile.value());
337 }
338 }
339}
340
Paul Stewart5baebb72013-03-14 11:43:29 -0700341bool L2TPIPSecDriver::InitPEMOptions(vector<string> *options) {
342 string ca_cert = args()->LookupString(kL2tpIpsecCaCertPemProperty, "");
343 if (ca_cert.empty()) {
344 return false;
345 }
346 FilePath certfile = certificate_file_->CreateDERFromString(ca_cert);
347 if (certfile.empty()) {
348 LOG(ERROR) << "Unable to extract certificate from PEM string.";
349 return false;
350 }
351 options->push_back("--server_ca_file");
352 options->push_back(certfile.value());
353 return true;
354}
355
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200356bool L2TPIPSecDriver::AppendValueOption(
357 const string &property, const string &option, vector<string> *options) {
Darin Petkov01c66042012-04-26 11:10:45 +0200358 string value = args()->LookupString(property, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200359 if (!value.empty()) {
360 options->push_back(option);
361 options->push_back(value);
362 return true;
363 }
364 return false;
365}
366
367bool L2TPIPSecDriver::AppendFlag(const string &property,
368 const string &true_option,
369 const string &false_option,
370 vector<string> *options) {
Darin Petkov01c66042012-04-26 11:10:45 +0200371 string value = args()->LookupString(property, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200372 if (!value.empty()) {
373 options->push_back(value == "true" ? true_option : false_option);
374 return true;
375 }
376 return false;
377}
378
Darin Petkov209e6292012-04-20 11:33:32 +0200379// static
380void L2TPIPSecDriver::OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data) {
Darin Petkov85d53172013-03-13 16:43:28 +0100381 LOG(INFO) << __func__ << "(" << pid << ", " << status << ")";
Darin Petkov209e6292012-04-20 11:33:32 +0200382 L2TPIPSecDriver *me = reinterpret_cast<L2TPIPSecDriver *>(data);
383 me->child_watch_tag_ = 0;
384 CHECK_EQ(pid, me->pid_);
Darin Petkov5a850472012-06-06 15:44:24 +0200385 me->pid_ = 0;
Darin Petkov85d53172013-03-13 16:43:28 +0100386 me->FailService(TranslateExitStatusToFailure(status));
Darin Petkov209e6292012-04-20 11:33:32 +0200387 // TODO(petkov): Figure if we need to restart the connection.
388}
389
Darin Petkov85d53172013-03-13 16:43:28 +0100390// static
391Service::ConnectFailure L2TPIPSecDriver::TranslateExitStatusToFailure(
392 int status) {
393 if (!WIFEXITED(status)) {
Darin Petkov1c049c72013-03-21 13:15:45 +0100394 return Service::kFailureInternal;
Darin Petkov85d53172013-03-13 16:43:28 +0100395 }
396 switch (WEXITSTATUS(status)) {
397 case vpn_manager::kServiceErrorResolveHostnameFailed:
398 return Service::kFailureDNSLookup;
399 case vpn_manager::kServiceErrorIpsecConnectionFailed:
400 case vpn_manager::kServiceErrorL2tpConnectionFailed:
401 case vpn_manager::kServiceErrorPppConnectionFailed:
402 return Service::kFailureConnect;
403 case vpn_manager::kServiceErrorIpsecPresharedKeyAuthenticationFailed:
404 return Service::kFailureIPSecPSKAuth;
405 case vpn_manager::kServiceErrorIpsecCertificateAuthenticationFailed:
406 return Service::kFailureIPSecCertAuth;
407 case vpn_manager::kServiceErrorPppAuthenticationFailed:
408 return Service::kFailurePPPAuth;
409 default:
410 break;
411 }
412 return Service::kFailureUnknown;
413}
414
Darin Petkov209e6292012-04-20 11:33:32 +0200415void L2TPIPSecDriver::GetLogin(string *user, string *password) {
Darin Petkov602303f2012-06-06 12:15:59 +0200416 LOG(INFO) << "Login requested.";
Darin Petkov209e6292012-04-20 11:33:32 +0200417 string user_property =
Darin Petkov01c66042012-04-26 11:10:45 +0200418 args()->LookupString(flimflam::kL2tpIpsecUserProperty, "");
Darin Petkov209e6292012-04-20 11:33:32 +0200419 if (user_property.empty()) {
420 LOG(ERROR) << "User not set.";
421 return;
422 }
423 string password_property =
Darin Petkov01c66042012-04-26 11:10:45 +0200424 args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "");
Darin Petkov209e6292012-04-20 11:33:32 +0200425 if (password_property.empty()) {
426 LOG(ERROR) << "Password not set.";
427 return;
428 }
429 *user = user_property;
430 *password = password_property;
431}
432
Darin Petkov0e9735d2012-04-24 12:33:45 +0200433void L2TPIPSecDriver::ParseIPConfiguration(
434 const map<string, string> &configuration,
435 IPConfig::Properties *properties,
436 string *interface_name) {
437 properties->address_family = IPAddress::kFamilyIPv4;
438 properties->subnet_prefix = IPAddress::GetMaxPrefixLength(
439 properties->address_family);
440 for (map<string, string>::const_iterator it = configuration.begin();
441 it != configuration.end(); ++it) {
442 const string &key = it->first;
443 const string &value = it->second;
444 SLOG(VPN, 2) << "Processing: " << key << " -> " << value;
Darin Petkov95f317f2012-10-22 13:37:43 +0200445 if (key == kL2TPIPSecInternalIP4Address) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200446 properties->address = value;
Darin Petkov95f317f2012-10-22 13:37:43 +0200447 } else if (key == kL2TPIPSecExternalIP4Address) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200448 properties->peer_address = value;
Darin Petkov95f317f2012-10-22 13:37:43 +0200449 } else if (key == kL2TPIPSecGatewayAddress) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200450 properties->gateway = value;
Darin Petkov95f317f2012-10-22 13:37:43 +0200451 } else if (key == kL2TPIPSecDNS1) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200452 properties->dns_servers.insert(properties->dns_servers.begin(), value);
Darin Petkov95f317f2012-10-22 13:37:43 +0200453 } else if (key == kL2TPIPSecDNS2) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200454 properties->dns_servers.push_back(value);
Darin Petkov95f317f2012-10-22 13:37:43 +0200455 } else if (key == kL2TPIPSecInterfaceName) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200456 *interface_name = value;
Darin Petkov95f317f2012-10-22 13:37:43 +0200457 } else if (key == kL2TPIPSecLNSAddress) {
Darin Petkov0e9735d2012-04-24 12:33:45 +0200458 properties->trusted_ip = value;
459 } else {
460 SLOG(VPN, 2) << "Key ignored.";
461 }
462 }
Ben Chana0163122012-09-25 15:10:52 -0700463
464 // There is no IPv6 support for L2TP/IPsec VPN at this moment, so create a
465 // blackhole route for IPv6 traffic after establishing a IPv4 VPN.
466 // TODO(benchan): Generalize this when IPv6 support is added.
467 properties->blackhole_ipv6 = true;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200468}
469
Darin Petkov209e6292012-04-20 11:33:32 +0200470void L2TPIPSecDriver::Notify(
471 const string &reason, const map<string, string> &dict) {
Darin Petkov602303f2012-06-06 12:15:59 +0200472 LOG(INFO) << "IP configuration received: " << reason;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200473
Darin Petkov95f317f2012-10-22 13:37:43 +0200474 if (reason != kL2TPIPSecReasonConnect) {
Darin Petkov69990222012-11-14 09:25:25 +0100475 DCHECK(reason == kL2TPIPSecReasonDisconnect);
476 // Avoid destroying the RPC task inside the adaptor callback by doing it
477 // from the main event loop.
478 dispatcher()->PostTask(Bind(&DeleteRPCTask, rpc_task_.release()));
Darin Petkov85d53172013-03-13 16:43:28 +0100479 FailService(Service::kFailureUnknown);
Darin Petkov0e9735d2012-04-24 12:33:45 +0200480 return;
481 }
482
Darin Petkovf8046b82012-04-24 16:29:23 +0200483 DeletePSKFile();
484
Darin Petkov0e9735d2012-04-24 12:33:45 +0200485 IPConfig::Properties properties;
486 string interface_name;
487 ParseIPConfiguration(dict, &properties, &interface_name);
Darin Petkovf8046b82012-04-24 16:29:23 +0200488
489 int interface_index = device_info_->GetIndex(interface_name);
490 if (interface_index < 0) {
491 // TODO(petkov): Consider handling the race when the RTNL notification about
492 // the new PPP device has not been received yet. We can keep the IP
Darin Petkovf8f970a2012-09-03 11:32:55 +0200493 // configuration and apply it when ClaimInterface is
494 // invoked. crosbug.com/29970.
Darin Petkovf8046b82012-04-24 16:29:23 +0200495 NOTIMPLEMENTED() << ": No device info for " << interface_name << ".";
496 return;
497 }
498
499 if (!device_) {
Darin Petkov602303f2012-06-06 12:15:59 +0200500 device_ = new VPN(control_, dispatcher(), metrics_, manager(),
Darin Petkovf8046b82012-04-24 16:29:23 +0200501 interface_name, interface_index);
502 }
503 device_->SetEnabled(true);
504 device_->SelectService(service_);
Darin Petkovf8046b82012-04-24 16:29:23 +0200505 device_->UpdateIPConfig(properties);
Paul Stewart91a43cb2013-03-02 21:34:15 -0800506 ReportConnectionMetrics();
Darin Petkov602303f2012-06-06 12:15:59 +0200507 StopConnectTimeout();
Darin Petkov209e6292012-04-20 11:33:32 +0200508}
509
Darin Petkov69990222012-11-14 09:25:25 +0100510// static
511void L2TPIPSecDriver::DeleteRPCTask(RPCTask *rpc_task) {
512 delete rpc_task;
513}
514
Darin Petkovb536a742012-04-26 11:31:28 +0200515KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) {
516 SLOG(VPN, 2) << __func__;
517 KeyValueStore props = VPNDriver::GetProvider(error);
518 props.SetBool(flimflam::kPassphraseRequiredProperty,
519 args()->LookupString(
520 flimflam::kL2tpIpsecPasswordProperty, "").empty());
521 props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty,
522 args()->LookupString(
523 flimflam::kL2tpIpsecPskProperty, "").empty());
524 return props;
525}
526
Paul Stewart91a43cb2013-03-02 21:34:15 -0800527void L2TPIPSecDriver::ReportConnectionMetrics() {
528 metrics_->SendEnumToUMA(
529 Metrics::kMetricVpnDriver,
530 Metrics::kVpnDriverL2tpIpsec,
531 Metrics::kMetricVpnDriverMax);
532
533 // We output an enum for each of the authentication types specified,
534 // even if more than one is set at the same time.
535 bool has_remote_authentication = false;
Paul Stewarte8e71da2013-03-20 08:48:33 -0700536 if (args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "") != "") {
Paul Stewart91a43cb2013-03-02 21:34:15 -0800537 metrics_->SendEnumToUMA(
538 Metrics::kMetricVpnRemoteAuthenticationType,
539 Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecCertificate,
540 Metrics::kMetricVpnRemoteAuthenticationTypeMax);
541 has_remote_authentication = true;
542 }
Paul Stewarte8e71da2013-03-20 08:48:33 -0700543 if (args()->LookupString(flimflam::kL2tpIpsecPskProperty, "") != "") {
Paul Stewart91a43cb2013-03-02 21:34:15 -0800544 metrics_->SendEnumToUMA(
545 Metrics::kMetricVpnRemoteAuthenticationType,
546 Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecPsk,
547 Metrics::kMetricVpnRemoteAuthenticationTypeMax);
548 has_remote_authentication = true;
549 }
550 if (!has_remote_authentication) {
551 metrics_->SendEnumToUMA(
552 Metrics::kMetricVpnRemoteAuthenticationType,
553 Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecDefault,
554 Metrics::kMetricVpnRemoteAuthenticationTypeMax);
555 }
556
557 bool has_user_authentication = false;
Paul Stewarte8e71da2013-03-20 08:48:33 -0700558 if (args()->LookupString(flimflam::kL2tpIpsecClientCertIdProperty,
559 "") != "") {
Paul Stewart91a43cb2013-03-02 21:34:15 -0800560 metrics_->SendEnumToUMA(
561 Metrics::kMetricVpnUserAuthenticationType,
562 Metrics::kVpnUserAuthenticationTypeL2tpIpsecCertificate,
563 Metrics::kMetricVpnUserAuthenticationTypeMax);
564 has_user_authentication = true;
565 }
Paul Stewarte8e71da2013-03-20 08:48:33 -0700566 if (args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "") != "") {
Paul Stewart91a43cb2013-03-02 21:34:15 -0800567 metrics_->SendEnumToUMA(
568 Metrics::kMetricVpnUserAuthenticationType,
569 Metrics::kVpnUserAuthenticationTypeL2tpIpsecUsernamePassword,
570 Metrics::kMetricVpnUserAuthenticationTypeMax);
571 has_user_authentication = true;
572 }
573 if (!has_user_authentication) {
574 metrics_->SendEnumToUMA(
575 Metrics::kMetricVpnUserAuthenticationType,
576 Metrics::kVpnUserAuthenticationTypeL2tpIpsecNone,
577 Metrics::kMetricVpnUserAuthenticationTypeMax);
578 }
579}
580
Darin Petkov7476a262012-04-12 16:30:46 +0200581} // namespace shill