blob: 269f114aca5be05b40d8ccaecc68a711ef1bce99 [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
5#include "shill/l2tp_ipsec_driver.h"
6
Darin Petkovf7ef50a2012-04-16 20:54:31 +02007#include <base/file_util.h>
Darin Petkov209e6292012-04-20 11:33:32 +02008#include <base/string_util.h>
Darin Petkov7476a262012-04-12 16:30:46 +02009#include <chromeos/dbus/service_constants.h>
10
Darin Petkovf8046b82012-04-24 16:29:23 +020011#include "shill/device_info.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020012#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070013#include "shill/logging.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020014#include "shill/manager.h"
15#include "shill/nss.h"
Darin Petkov5a850472012-06-06 15:44:24 +020016#include "shill/process_killer.h"
Darin Petkovf8046b82012-04-24 16:29:23 +020017#include "shill/vpn.h"
Darin Petkov209e6292012-04-20 11:33:32 +020018#include "shill/vpn_service.h"
Darin Petkovf7ef50a2012-04-16 20:54:31 +020019
Darin Petkov5a850472012-06-06 15:44:24 +020020using base::Closure;
Darin Petkov209e6292012-04-20 11:33:32 +020021using std::map;
Darin Petkov7476a262012-04-12 16:30:46 +020022using std::string;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020023using std::vector;
Darin Petkov7476a262012-04-12 16:30:46 +020024
25namespace shill {
26
Darin Petkovf7ef50a2012-04-16 20:54:31 +020027namespace {
28const char kL2TPIPSecIPSecTimeoutProperty[] = "L2TPIPsec.IPsecTimeout";
29const char kL2TPIPSecLeftProtoPortProperty[] = "L2TPIPsec.LeftProtoPort";
30const char kL2TPIPSecLengthBitProperty[] = "L2TPIPsec.LengthBit";
31const char kL2TPIPSecPFSProperty[] = "L2TPIPsec.PFS";
32const char kL2TPIPSecRefusePapProperty[] = "L2TPIPsec.RefusePap";
33const char kL2TPIPSecRekeyProperty[] = "L2TPIPsec.Rekey";
34const char kL2TPIPSecRequireAuthProperty[] = "L2TPIPsec.RequireAuth";
35const char kL2TPIPSecRequireChapProperty[] = "L2TPIPsec.RequireChap";
36const char kL2TPIPSecRightProtoPortProperty[] = "L2TPIPsec.RightProtoPort";
37} // namespace
38
39// static
40const char L2TPIPSecDriver::kPPPDPlugin[] = SCRIPTDIR "/libppp-plugin.so";
Darin Petkovd4325392012-04-23 15:48:22 +020041// static
Darin Petkov209e6292012-04-20 11:33:32 +020042const char L2TPIPSecDriver::kL2TPIPSecVPNPath[] = "/usr/sbin/l2tpipsec_vpn";
Darin Petkovd4325392012-04-23 15:48:22 +020043// static
44const VPNDriver::Property L2TPIPSecDriver::kProperties[] = {
45 { flimflam::kL2tpIpsecCaCertNssProperty, 0 },
46 { flimflam::kL2tpIpsecClientCertIdProperty, 0 },
47 { flimflam::kL2tpIpsecClientCertSlotProperty, 0 },
Darin Petkov02236552012-06-11 13:15:19 +020048 { flimflam::kL2tpIpsecPasswordProperty,
49 Property::kCredential | Property::kWriteOnly },
Darin Petkovcb715292012-04-25 13:04:37 +020050 { flimflam::kL2tpIpsecPinProperty, Property::kCredential },
51 { flimflam::kL2tpIpsecPskProperty, Property::kCredential },
Darin Petkovd4325392012-04-23 15:48:22 +020052 { flimflam::kL2tpIpsecUserProperty, 0 },
Darin Petkov2c773c22012-04-26 12:54:11 +020053 { flimflam::kProviderHostProperty, 0 },
54 { flimflam::kProviderNameProperty, 0 },
55 { flimflam::kProviderTypeProperty, 0 },
Darin Petkovd4325392012-04-23 15:48:22 +020056 { kL2TPIPSecIPSecTimeoutProperty, 0 },
57 { kL2TPIPSecLeftProtoPortProperty, 0 },
58 { kL2TPIPSecLengthBitProperty, 0 },
59 { kL2TPIPSecPFSProperty, 0 },
60 { kL2TPIPSecRefusePapProperty, 0 },
61 { kL2TPIPSecRekeyProperty, 0 },
62 { kL2TPIPSecRequireAuthProperty, 0 },
63 { kL2TPIPSecRequireChapProperty, 0 },
64 { kL2TPIPSecRightProtoPortProperty, 0 },
65};
Darin Petkovf7ef50a2012-04-16 20:54:31 +020066
Darin Petkov209e6292012-04-20 11:33:32 +020067L2TPIPSecDriver::L2TPIPSecDriver(ControlInterface *control,
Darin Petkovf8046b82012-04-24 16:29:23 +020068 EventDispatcher *dispatcher,
69 Metrics *metrics,
Darin Petkov209e6292012-04-20 11:33:32 +020070 Manager *manager,
Darin Petkovf8046b82012-04-24 16:29:23 +020071 DeviceInfo *device_info,
Darin Petkov209e6292012-04-20 11:33:32 +020072 GLib *glib)
Darin Petkov602303f2012-06-06 12:15:59 +020073 : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)),
Darin Petkovb451d6e2012-04-23 11:56:41 +020074 control_(control),
Darin Petkovf8046b82012-04-24 16:29:23 +020075 metrics_(metrics),
76 device_info_(device_info),
Darin Petkov209e6292012-04-20 11:33:32 +020077 glib_(glib),
78 nss_(NSS::GetInstance()),
Darin Petkov5a850472012-06-06 15:44:24 +020079 process_killer_(ProcessKiller::GetInstance()),
Darin Petkov209e6292012-04-20 11:33:32 +020080 pid_(0),
81 child_watch_tag_(0) {}
Darin Petkov7476a262012-04-12 16:30:46 +020082
Darin Petkov209e6292012-04-20 11:33:32 +020083L2TPIPSecDriver::~L2TPIPSecDriver() {
84 Cleanup(Service::kStateIdle);
85}
Darin Petkov7476a262012-04-12 16:30:46 +020086
87bool L2TPIPSecDriver::ClaimInterface(const string &link_name,
88 int interface_index) {
89 // TODO(petkov): crosbug.com/26843.
90 NOTIMPLEMENTED();
91 return false;
92}
93
94void L2TPIPSecDriver::Connect(const VPNServiceRefPtr &service, Error *error) {
Darin Petkov602303f2012-06-06 12:15:59 +020095 StartConnectTimeout();
Darin Petkov209e6292012-04-20 11:33:32 +020096 service_ = service;
97 service_->SetState(Service::kStateConfiguring);
98 rpc_task_.reset(new RPCTask(control_, this));
99 if (!SpawnL2TPIPSecVPN(error)) {
100 Cleanup(Service::kStateFailure);
101 }
Darin Petkov7476a262012-04-12 16:30:46 +0200102}
103
104void L2TPIPSecDriver::Disconnect() {
Darin Petkova0e645e2012-04-25 11:38:59 +0200105 SLOG(VPN, 2) << __func__;
106 Cleanup(Service::kStateIdle);
Darin Petkov7476a262012-04-12 16:30:46 +0200107}
108
Darin Petkov5eb05422012-05-11 15:45:25 +0200109void L2TPIPSecDriver::OnConnectionDisconnected() {
Darin Petkov602303f2012-06-06 12:15:59 +0200110 LOG(ERROR) << "VPN connection disconnected.";
Darin Petkov5eb05422012-05-11 15:45:25 +0200111 Cleanup(Service::kStateFailure);
112}
113
Darin Petkov7476a262012-04-12 16:30:46 +0200114string L2TPIPSecDriver::GetProviderType() const {
115 return flimflam::kProviderL2tpIpsec;
116}
117
Darin Petkov209e6292012-04-20 11:33:32 +0200118void L2TPIPSecDriver::Cleanup(Service::ConnectState state) {
119 SLOG(VPN, 2) << __func__
120 << "(" << Service::ConnectStateToString(state) << ")";
Darin Petkov602303f2012-06-06 12:15:59 +0200121 StopConnectTimeout();
Darin Petkov0e9735d2012-04-24 12:33:45 +0200122 DeletePSKFile();
Darin Petkov209e6292012-04-20 11:33:32 +0200123 if (child_watch_tag_) {
124 glib_->SourceRemove(child_watch_tag_);
125 child_watch_tag_ = 0;
Darin Petkov209e6292012-04-20 11:33:32 +0200126 }
127 if (pid_) {
Darin Petkov5a850472012-06-06 15:44:24 +0200128 process_killer_->Kill(pid_, Closure());
Darin Petkov209e6292012-04-20 11:33:32 +0200129 pid_ = 0;
130 }
Darin Petkovf8046b82012-04-24 16:29:23 +0200131 if (device_) {
132 device_->OnDisconnected();
133 device_->SetEnabled(false);
134 device_ = NULL;
135 }
Darin Petkov209e6292012-04-20 11:33:32 +0200136 rpc_task_.reset();
137 if (service_) {
138 service_->SetState(state);
139 service_ = NULL;
140 }
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200141}
142
Darin Petkov0e9735d2012-04-24 12:33:45 +0200143void L2TPIPSecDriver::DeletePSKFile() {
144 if (!psk_file_.empty()) {
145 file_util::Delete(psk_file_, false);
146 psk_file_.clear();
147 }
148}
149
Darin Petkov209e6292012-04-20 11:33:32 +0200150bool L2TPIPSecDriver::SpawnL2TPIPSecVPN(Error *error) {
151 SLOG(VPN, 2) << __func__;
152
153 vector<string> options;
154 if (!InitOptions(&options, error)) {
155 return false;
156 }
157 SLOG(VPN, 2) << "L2TP/IPSec VPN process options: "
158 << JoinString(options, ' ');
159
160 // TODO(petkov): This code needs to be abstracted away in a separate external
161 // process module (crosbug.com/27131).
162 vector<char *> process_args;
163 process_args.push_back(const_cast<char *>(kL2TPIPSecVPNPath));
164 for (vector<string>::const_iterator it = options.begin();
165 it != options.end(); ++it) {
166 process_args.push_back(const_cast<char *>(it->c_str()));
167 }
168 process_args.push_back(NULL);
169
170 vector<string> environment;
171 InitEnvironment(&environment);
172
173 vector<char *> process_env;
174 for (vector<string>::const_iterator it = environment.begin();
175 it != environment.end(); ++it) {
176 process_env.push_back(const_cast<char *>(it->c_str()));
177 }
178 process_env.push_back(NULL);
179
180 CHECK(!pid_);
181 // Redirect all l2tp/ipsec output to stderr.
182 int stderr_fd = fileno(stderr);
183 if (!glib_->SpawnAsyncWithPipesCWD(process_args.data(),
184 process_env.data(),
185 G_SPAWN_DO_NOT_REAP_CHILD,
186 NULL,
187 NULL,
188 &pid_,
189 NULL,
190 &stderr_fd,
191 &stderr_fd,
192 NULL)) {
193 Error::PopulateAndLog(error, Error::kInternalError,
194 string("Unable to spawn: ") + process_args[0]);
195 return false;
196 }
197 CHECK(!child_watch_tag_);
198 child_watch_tag_ = glib_->ChildWatchAdd(pid_, OnL2TPIPSecVPNDied, this);
199 return true;
200}
201
202void L2TPIPSecDriver::InitEnvironment(vector<string> *environment) {
203 environment->push_back(
204 "CONNMAN_BUSNAME=" + rpc_task_->GetRpcConnectionIdentifier());
205 environment->push_back(
206 "CONNMAN_INTERFACE=" + rpc_task_->GetRpcInterfaceIdentifier());
207 environment->push_back(
208 "CONNMAN_PATH=" + rpc_task_->GetRpcIdentifier());
209}
210
211bool L2TPIPSecDriver::InitOptions(vector<string> *options, Error *error) {
Darin Petkov01c66042012-04-26 11:10:45 +0200212 string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200213 if (vpnhost.empty()) {
214 Error::PopulateAndLog(
215 error, Error::kInvalidArguments, "VPN host not specified.");
Darin Petkov209e6292012-04-20 11:33:32 +0200216 return false;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200217 }
218
219 if (!InitPSKOptions(options, error)) {
Darin Petkov209e6292012-04-20 11:33:32 +0200220 return false;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200221 }
222
223 options->push_back("--remote_host");
224 options->push_back(vpnhost);
225 options->push_back("--pppd_plugin");
226 options->push_back(kPPPDPlugin);
227 // Disable pppd from configuring IP addresses, routes, DNS.
228 options->push_back("--nosystemconfig");
229
230 InitNSSOptions(options);
231
232 AppendValueOption(flimflam::kL2tpIpsecClientCertIdProperty,
233 "--client_cert_id", options);
234 AppendValueOption(flimflam::kL2tpIpsecClientCertSlotProperty,
235 "--client_cert_slot", options);
236 AppendValueOption(flimflam::kL2tpIpsecPinProperty, "--user_pin", options);
237 AppendValueOption(flimflam::kL2tpIpsecUserProperty, "--user", options);
238 AppendValueOption(kL2TPIPSecIPSecTimeoutProperty, "--ipsec_timeout", options);
239 AppendValueOption(kL2TPIPSecLeftProtoPortProperty,
240 "--leftprotoport", options);
241 AppendFlag(kL2TPIPSecPFSProperty, "--pfs", "--nopfs", options);
242 AppendFlag(kL2TPIPSecRekeyProperty, "--rekey", "--norekey", options);
243 AppendValueOption(kL2TPIPSecRightProtoPortProperty,
244 "--rightprotoport", options);
245 AppendFlag(kL2TPIPSecRequireChapProperty,
246 "--require_chap", "--norequire_chap", options);
247 AppendFlag(kL2TPIPSecRefusePapProperty,
248 "--refuse_pap", "--norefuse_pap", options);
249 AppendFlag(kL2TPIPSecRequireAuthProperty,
250 "--require_authentication", "--norequire_authentication", options);
251 AppendFlag(kL2TPIPSecLengthBitProperty,
252 "--length_bit", "--nolength_bit", options);
Darin Petkov209e6292012-04-20 11:33:32 +0200253 if (SLOG_IS_ON(VPN, 0)) {
254 options->push_back("--debug");
255 }
256 return true;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200257}
258
259bool L2TPIPSecDriver::InitPSKOptions(vector<string> *options, Error *error) {
Darin Petkov01c66042012-04-26 11:10:45 +0200260 string psk = args()->LookupString(flimflam::kL2tpIpsecPskProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200261 if (!psk.empty()) {
262 if (!file_util::CreateTemporaryFileInDir(
Darin Petkov0e9735d2012-04-24 12:33:45 +0200263 manager()->run_path(), &psk_file_) ||
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200264 chmod(psk_file_.value().c_str(), S_IRUSR | S_IWUSR) ||
265 file_util::WriteFile(psk_file_, psk.data(), psk.size()) !=
266 static_cast<int>(psk.size())) {
267 Error::PopulateAndLog(
268 error, Error::kInternalError, "Unable to setup psk file.");
269 return false;
270 }
271 options->push_back("--psk_file");
272 options->push_back(psk_file_.value());
273 }
274 return true;
275}
276
277void L2TPIPSecDriver::InitNSSOptions(vector<string> *options) {
278 string ca_cert =
Darin Petkov01c66042012-04-26 11:10:45 +0200279 args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200280 if (!ca_cert.empty()) {
Darin Petkov01c66042012-04-26 11:10:45 +0200281 const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty);
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200282 vector<char> id(vpnhost.begin(), vpnhost.end());
283 FilePath certfile = nss_->GetDERCertfile(ca_cert, id);
284 if (certfile.empty()) {
285 LOG(ERROR) << "Unable to extract certificate: " << ca_cert;
286 } else {
287 options->push_back("--server_ca_file");
288 options->push_back(certfile.value());
289 }
290 }
291}
292
293bool L2TPIPSecDriver::AppendValueOption(
294 const string &property, const string &option, vector<string> *options) {
Darin Petkov01c66042012-04-26 11:10:45 +0200295 string value = args()->LookupString(property, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200296 if (!value.empty()) {
297 options->push_back(option);
298 options->push_back(value);
299 return true;
300 }
301 return false;
302}
303
304bool L2TPIPSecDriver::AppendFlag(const string &property,
305 const string &true_option,
306 const string &false_option,
307 vector<string> *options) {
Darin Petkov01c66042012-04-26 11:10:45 +0200308 string value = args()->LookupString(property, "");
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200309 if (!value.empty()) {
310 options->push_back(value == "true" ? true_option : false_option);
311 return true;
312 }
313 return false;
314}
315
Darin Petkov209e6292012-04-20 11:33:32 +0200316// static
317void L2TPIPSecDriver::OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data) {
318 SLOG(VPN, 2) << __func__ << "(" << pid << ", " << status << ")";
319 L2TPIPSecDriver *me = reinterpret_cast<L2TPIPSecDriver *>(data);
320 me->child_watch_tag_ = 0;
321 CHECK_EQ(pid, me->pid_);
Darin Petkov5a850472012-06-06 15:44:24 +0200322 me->pid_ = 0;
Darin Petkov209e6292012-04-20 11:33:32 +0200323 me->Cleanup(Service::kStateFailure);
324 // TODO(petkov): Figure if we need to restart the connection.
325}
326
327void L2TPIPSecDriver::GetLogin(string *user, string *password) {
Darin Petkov602303f2012-06-06 12:15:59 +0200328 LOG(INFO) << "Login requested.";
Darin Petkov209e6292012-04-20 11:33:32 +0200329 string user_property =
Darin Petkov01c66042012-04-26 11:10:45 +0200330 args()->LookupString(flimflam::kL2tpIpsecUserProperty, "");
Darin Petkov209e6292012-04-20 11:33:32 +0200331 if (user_property.empty()) {
332 LOG(ERROR) << "User not set.";
333 return;
334 }
335 string password_property =
Darin Petkov01c66042012-04-26 11:10:45 +0200336 args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "");
Darin Petkov209e6292012-04-20 11:33:32 +0200337 if (password_property.empty()) {
338 LOG(ERROR) << "Password not set.";
339 return;
340 }
341 *user = user_property;
342 *password = password_property;
343}
344
Darin Petkov0e9735d2012-04-24 12:33:45 +0200345void L2TPIPSecDriver::ParseIPConfiguration(
346 const map<string, string> &configuration,
347 IPConfig::Properties *properties,
348 string *interface_name) {
349 properties->address_family = IPAddress::kFamilyIPv4;
350 properties->subnet_prefix = IPAddress::GetMaxPrefixLength(
351 properties->address_family);
352 for (map<string, string>::const_iterator it = configuration.begin();
353 it != configuration.end(); ++it) {
354 const string &key = it->first;
355 const string &value = it->second;
356 SLOG(VPN, 2) << "Processing: " << key << " -> " << value;
357 if (key == "INTERNAL_IP4_ADDRESS") {
358 properties->address = value;
359 } else if (key == "EXTERNAL_IP4_ADDRESS") {
360 properties->peer_address = value;
361 } else if (key == "GATEWAY_ADDRESS") {
362 properties->gateway = value;
363 } else if (key == "DNS1") {
364 properties->dns_servers.insert(properties->dns_servers.begin(), value);
365 } else if (key == "DNS2") {
366 properties->dns_servers.push_back(value);
367 } else if (key == "INTERNAL_IFNAME") {
368 *interface_name = value;
369 } else if (key == "LNS_ADDRESS") {
370 properties->trusted_ip = value;
371 } else {
372 SLOG(VPN, 2) << "Key ignored.";
373 }
374 }
375}
376
Darin Petkov209e6292012-04-20 11:33:32 +0200377void L2TPIPSecDriver::Notify(
378 const string &reason, const map<string, string> &dict) {
Darin Petkov602303f2012-06-06 12:15:59 +0200379 LOG(INFO) << "IP configuration received: " << reason;
Darin Petkov0e9735d2012-04-24 12:33:45 +0200380
381 if (reason != "connect") {
Darin Petkova0e645e2012-04-25 11:38:59 +0200382 device_->OnDisconnected();
Darin Petkov0e9735d2012-04-24 12:33:45 +0200383 return;
384 }
385
Darin Petkovf8046b82012-04-24 16:29:23 +0200386 DeletePSKFile();
387
Darin Petkov0e9735d2012-04-24 12:33:45 +0200388 IPConfig::Properties properties;
389 string interface_name;
390 ParseIPConfiguration(dict, &properties, &interface_name);
Darin Petkovf8046b82012-04-24 16:29:23 +0200391
392 int interface_index = device_info_->GetIndex(interface_name);
393 if (interface_index < 0) {
394 // TODO(petkov): Consider handling the race when the RTNL notification about
395 // the new PPP device has not been received yet. We can keep the IP
396 // configuration and apply it when ClaimInterface is invoked.
397 NOTIMPLEMENTED() << ": No device info for " << interface_name << ".";
398 return;
399 }
400
401 if (!device_) {
Darin Petkov602303f2012-06-06 12:15:59 +0200402 device_ = new VPN(control_, dispatcher(), metrics_, manager(),
Darin Petkovf8046b82012-04-24 16:29:23 +0200403 interface_name, interface_index);
404 }
405 device_->SetEnabled(true);
406 device_->SelectService(service_);
Darin Petkovf8046b82012-04-24 16:29:23 +0200407 device_->UpdateIPConfig(properties);
Darin Petkov602303f2012-06-06 12:15:59 +0200408 StopConnectTimeout();
Darin Petkov209e6292012-04-20 11:33:32 +0200409}
410
Darin Petkovb536a742012-04-26 11:31:28 +0200411KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) {
412 SLOG(VPN, 2) << __func__;
413 KeyValueStore props = VPNDriver::GetProvider(error);
414 props.SetBool(flimflam::kPassphraseRequiredProperty,
415 args()->LookupString(
416 flimflam::kL2tpIpsecPasswordProperty, "").empty());
417 props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty,
418 args()->LookupString(
419 flimflam::kL2tpIpsecPskProperty, "").empty());
420 return props;
421}
422
Darin Petkov7476a262012-04-12 16:30:46 +0200423} // namespace shill