Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [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 | |
Paul Stewart | 5bbf93b | 2013-03-21 12:06:08 -0700 | [diff] [blame] | 5 | // 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 Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 24 | #include "shill/l2tp_ipsec_driver.h" |
| 25 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 26 | #include <base/bind.h> |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 27 | #include <base/file_util.h> |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 28 | #include <base/string_util.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 29 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 30 | #include <chromeos/vpn-manager/service_error.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 31 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 32 | #include "shill/certificate_file.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 33 | #include "shill/device_info.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 34 | #include "shill/error.h" |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 35 | #include "shill/external_task.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 36 | #include "shill/logging.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 37 | #include "shill/manager.h" |
| 38 | #include "shill/nss.h" |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 39 | #include "shill/ppp_device.h" |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 40 | #include "shill/vpn_service.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 41 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 42 | using base::Bind; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 43 | using base::Closure; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 44 | using base::FilePath; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 45 | using std::map; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 46 | using std::string; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 47 | using std::vector; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 48 | |
| 49 | namespace shill { |
| 50 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 51 | namespace { |
| 52 | const char kL2TPIPSecIPSecTimeoutProperty[] = "L2TPIPsec.IPsecTimeout"; |
| 53 | const char kL2TPIPSecLeftProtoPortProperty[] = "L2TPIPsec.LeftProtoPort"; |
| 54 | const char kL2TPIPSecLengthBitProperty[] = "L2TPIPsec.LengthBit"; |
| 55 | const char kL2TPIPSecPFSProperty[] = "L2TPIPsec.PFS"; |
| 56 | const char kL2TPIPSecRefusePapProperty[] = "L2TPIPsec.RefusePap"; |
| 57 | const char kL2TPIPSecRekeyProperty[] = "L2TPIPsec.Rekey"; |
| 58 | const char kL2TPIPSecRequireAuthProperty[] = "L2TPIPsec.RequireAuth"; |
| 59 | const char kL2TPIPSecRequireChapProperty[] = "L2TPIPsec.RequireChap"; |
| 60 | const char kL2TPIPSecRightProtoPortProperty[] = "L2TPIPsec.RightProtoPort"; |
| 61 | } // namespace |
| 62 | |
| 63 | // static |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 64 | const char L2TPIPSecDriver::kL2TPIPSecVPNPath[] = "/usr/sbin/l2tpipsec_vpn"; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 65 | // static |
| 66 | const VPNDriver::Property L2TPIPSecDriver::kProperties[] = { |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 67 | { flimflam::kL2tpIpsecAuthenticationType, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 68 | { flimflam::kL2tpIpsecCaCertNssProperty, 0 }, |
| 69 | { flimflam::kL2tpIpsecClientCertIdProperty, 0 }, |
| 70 | { flimflam::kL2tpIpsecClientCertSlotProperty, 0 }, |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 71 | { flimflam::kL2tpIpsecIkeVersion, 0 }, |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 72 | { flimflam::kL2tpIpsecPasswordProperty, |
| 73 | Property::kCredential | Property::kWriteOnly }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 74 | { flimflam::kL2tpIpsecPinProperty, Property::kCredential }, |
| 75 | { flimflam::kL2tpIpsecPskProperty, Property::kCredential }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 76 | { flimflam::kL2tpIpsecUserProperty, 0 }, |
Darin Petkov | 2c773c2 | 2012-04-26 12:54:11 +0200 | [diff] [blame] | 77 | { flimflam::kProviderHostProperty, 0 }, |
Darin Petkov | 2c773c2 | 2012-04-26 12:54:11 +0200 | [diff] [blame] | 78 | { flimflam::kProviderTypeProperty, 0 }, |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 79 | { kL2tpIpsecCaCertPemProperty, Property::kArray }, |
Paul Stewart | 5bbf93b | 2013-03-21 12:06:08 -0700 | [diff] [blame] | 80 | { kL2tpIpsecTunnelGroupProperty, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 81 | { kL2TPIPSecIPSecTimeoutProperty, 0 }, |
| 82 | { kL2TPIPSecLeftProtoPortProperty, 0 }, |
| 83 | { kL2TPIPSecLengthBitProperty, 0 }, |
| 84 | { kL2TPIPSecPFSProperty, 0 }, |
| 85 | { kL2TPIPSecRefusePapProperty, 0 }, |
| 86 | { kL2TPIPSecRekeyProperty, 0 }, |
| 87 | { kL2TPIPSecRequireAuthProperty, 0 }, |
| 88 | { kL2TPIPSecRequireChapProperty, 0 }, |
| 89 | { kL2TPIPSecRightProtoPortProperty, 0 }, |
| 90 | }; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 91 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 92 | L2TPIPSecDriver::L2TPIPSecDriver(ControlInterface *control, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 93 | EventDispatcher *dispatcher, |
| 94 | Metrics *metrics, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 95 | Manager *manager, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 96 | DeviceInfo *device_info, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 97 | GLib *glib) |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 98 | : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)), |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 99 | control_(control), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 100 | metrics_(metrics), |
| 101 | device_info_(device_info), |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 102 | glib_(glib), |
| 103 | nss_(NSS::GetInstance()), |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 104 | certificate_file_(new CertificateFile()), |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 105 | weak_ptr_factory_(this) {} |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 106 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 107 | L2TPIPSecDriver::~L2TPIPSecDriver() { |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 108 | IdleService(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 109 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 110 | |
| 111 | bool L2TPIPSecDriver::ClaimInterface(const string &link_name, |
| 112 | int interface_index) { |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 113 | // TODO(petkov): crbug.com/212446. |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 114 | NOTIMPLEMENTED(); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | void L2TPIPSecDriver::Connect(const VPNServiceRefPtr &service, Error *error) { |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 119 | StartConnectTimeout(kDefaultConnectTimeoutSeconds); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 120 | service_ = service; |
| 121 | service_->SetState(Service::kStateConfiguring); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 122 | if (!SpawnL2TPIPSecVPN(error)) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 123 | FailService(Service::kFailureInternal); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 124 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void L2TPIPSecDriver::Disconnect() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 128 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 129 | IdleService(); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 132 | void L2TPIPSecDriver::OnConnectionDisconnected() { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 133 | LOG(INFO) << "Underlying connection disconnected."; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 134 | IdleService(); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void L2TPIPSecDriver::OnConnectTimeout() { |
| 138 | VPNDriver::OnConnectTimeout(); |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 139 | FailService(Service::kFailureConnect); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 142 | string L2TPIPSecDriver::GetProviderType() const { |
| 143 | return flimflam::kProviderL2tpIpsec; |
| 144 | } |
| 145 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 146 | void L2TPIPSecDriver::IdleService() { |
| 147 | Cleanup(Service::kStateIdle, Service::kFailureUnknown); |
| 148 | } |
| 149 | |
| 150 | void L2TPIPSecDriver::FailService(Service::ConnectFailure failure) { |
| 151 | Cleanup(Service::kStateFailure, failure); |
| 152 | } |
| 153 | |
| 154 | void L2TPIPSecDriver::Cleanup(Service::ConnectState state, |
| 155 | Service::ConnectFailure failure) { |
| 156 | SLOG(VPN, 2) << __func__ << "(" |
| 157 | << Service::ConnectStateToString(state) << ", " |
| 158 | << Service::ConnectFailureToString(failure) << ")"; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 159 | StopConnectTimeout(); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 160 | DeletePSKFile(); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 161 | external_task_.reset(); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 162 | if (device_) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 163 | device_->DropConnection(); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 164 | device_->SetEnabled(false); |
| 165 | device_ = NULL; |
| 166 | } |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 167 | if (service_) { |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 168 | if (state == Service::kStateFailure) { |
| 169 | service_->SetFailure(failure); |
| 170 | } else { |
| 171 | service_->SetState(state); |
| 172 | } |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 173 | service_ = NULL; |
| 174 | } |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 177 | void L2TPIPSecDriver::DeletePSKFile() { |
| 178 | if (!psk_file_.empty()) { |
| 179 | file_util::Delete(psk_file_, false); |
| 180 | psk_file_.clear(); |
| 181 | } |
| 182 | } |
| 183 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 184 | bool L2TPIPSecDriver::SpawnL2TPIPSecVPN(Error *error) { |
| 185 | SLOG(VPN, 2) << __func__; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 186 | scoped_ptr<ExternalTask> external_task_local( |
| 187 | new ExternalTask(control_, glib_, |
| 188 | weak_ptr_factory_.GetWeakPtr(), |
| 189 | Bind(&L2TPIPSecDriver::OnL2TPIPSecVPNDied, |
| 190 | weak_ptr_factory_.GetWeakPtr()))); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 191 | |
| 192 | vector<string> options; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 193 | map<string, string> environment; // No env vars passed. |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 194 | if (!InitOptions(&options, error)) { |
| 195 | return false; |
| 196 | } |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 197 | LOG(INFO) << "L2TP/IPSec VPN process options: " << JoinString(options, ' '); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 198 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 199 | if (external_task_local->Start( |
| 200 | FilePath(kL2TPIPSecVPNPath), options, environment, error)) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 201 | external_task_ = external_task_local.Pass(); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 202 | return true; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 203 | } |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 204 | return false; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | bool L2TPIPSecDriver::InitOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 208 | string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 209 | if (vpnhost.empty()) { |
| 210 | Error::PopulateAndLog( |
| 211 | error, Error::kInvalidArguments, "VPN host not specified."); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 212 | return false; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (!InitPSKOptions(options, error)) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 216 | return false; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | options->push_back("--remote_host"); |
| 220 | options->push_back(vpnhost); |
| 221 | options->push_back("--pppd_plugin"); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 222 | options->push_back(PPPDevice::kPluginPath); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 223 | // Disable pppd from configuring IP addresses, routes, DNS. |
| 224 | options->push_back("--nosystemconfig"); |
| 225 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 226 | // Accept a PEM CA certificate or an NSS certificate, but not both. |
| 227 | // Prefer PEM to NSS. |
| 228 | if (!InitPEMOptions(options)) { |
| 229 | InitNSSOptions(options); |
| 230 | } |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 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); |
Paul Stewart | 5bbf93b | 2013-03-21 12:06:08 -0700 | [diff] [blame] | 253 | AppendValueOption(kL2tpIpsecTunnelGroupProperty, "--tunnel_group", options); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 254 | if (SLOG_IS_ON(VPN, 0)) { |
| 255 | options->push_back("--debug"); |
| 256 | } |
| 257 | return true; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | bool L2TPIPSecDriver::InitPSKOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 261 | string psk = args()->LookupString(flimflam::kL2tpIpsecPskProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 262 | if (!psk.empty()) { |
| 263 | if (!file_util::CreateTemporaryFileInDir( |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 264 | manager()->run_path(), &psk_file_) || |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 265 | chmod(psk_file_.value().c_str(), S_IRUSR | S_IWUSR) || |
| 266 | file_util::WriteFile(psk_file_, psk.data(), psk.size()) != |
| 267 | static_cast<int>(psk.size())) { |
| 268 | Error::PopulateAndLog( |
| 269 | error, Error::kInternalError, "Unable to setup psk file."); |
| 270 | return false; |
| 271 | } |
| 272 | options->push_back("--psk_file"); |
| 273 | options->push_back(psk_file_.value()); |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | void L2TPIPSecDriver::InitNSSOptions(vector<string> *options) { |
| 279 | string ca_cert = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 280 | args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 281 | if (!ca_cert.empty()) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 282 | const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 283 | vector<char> id(vpnhost.begin(), vpnhost.end()); |
| 284 | FilePath certfile = nss_->GetDERCertfile(ca_cert, id); |
| 285 | if (certfile.empty()) { |
| 286 | LOG(ERROR) << "Unable to extract certificate: " << ca_cert; |
| 287 | } else { |
| 288 | options->push_back("--server_ca_file"); |
| 289 | options->push_back(certfile.value()); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 294 | bool L2TPIPSecDriver::InitPEMOptions(vector<string> *options) { |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 295 | vector<string> ca_certs; |
| 296 | if (args()->ContainsStrings(kL2tpIpsecCaCertPemProperty)) { |
| 297 | ca_certs = args()->GetStrings(kL2tpIpsecCaCertPemProperty); |
| 298 | } |
| 299 | if (ca_certs.empty()) { |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 300 | return false; |
| 301 | } |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 302 | FilePath certfile = certificate_file_->CreatePEMFromStrings(ca_certs); |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 303 | if (certfile.empty()) { |
Paul Stewart | eb713e8 | 2013-06-28 14:51:54 -0700 | [diff] [blame] | 304 | LOG(ERROR) << "Unable to extract certificates from PEM string."; |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | options->push_back("--server_ca_file"); |
| 308 | options->push_back(certfile.value()); |
| 309 | return true; |
| 310 | } |
| 311 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 312 | bool L2TPIPSecDriver::AppendValueOption( |
| 313 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 314 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 315 | if (!value.empty()) { |
| 316 | options->push_back(option); |
| 317 | options->push_back(value); |
| 318 | return true; |
| 319 | } |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | bool L2TPIPSecDriver::AppendFlag(const string &property, |
| 324 | const string &true_option, |
| 325 | const string &false_option, |
| 326 | vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 327 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 328 | if (!value.empty()) { |
| 329 | options->push_back(value == "true" ? true_option : false_option); |
| 330 | return true; |
| 331 | } |
| 332 | return false; |
| 333 | } |
| 334 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 335 | void L2TPIPSecDriver::OnL2TPIPSecVPNDied(pid_t /*pid*/, int status) { |
| 336 | FailService(TranslateExitStatusToFailure(status)); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 337 | // TODO(petkov): Figure if we need to restart the connection. |
| 338 | } |
| 339 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 340 | // static |
| 341 | Service::ConnectFailure L2TPIPSecDriver::TranslateExitStatusToFailure( |
| 342 | int status) { |
| 343 | if (!WIFEXITED(status)) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 344 | return Service::kFailureInternal; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 345 | } |
| 346 | switch (WEXITSTATUS(status)) { |
| 347 | case vpn_manager::kServiceErrorResolveHostnameFailed: |
| 348 | return Service::kFailureDNSLookup; |
| 349 | case vpn_manager::kServiceErrorIpsecConnectionFailed: |
| 350 | case vpn_manager::kServiceErrorL2tpConnectionFailed: |
| 351 | case vpn_manager::kServiceErrorPppConnectionFailed: |
| 352 | return Service::kFailureConnect; |
| 353 | case vpn_manager::kServiceErrorIpsecPresharedKeyAuthenticationFailed: |
| 354 | return Service::kFailureIPSecPSKAuth; |
| 355 | case vpn_manager::kServiceErrorIpsecCertificateAuthenticationFailed: |
| 356 | return Service::kFailureIPSecCertAuth; |
| 357 | case vpn_manager::kServiceErrorPppAuthenticationFailed: |
| 358 | return Service::kFailurePPPAuth; |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | return Service::kFailureUnknown; |
| 363 | } |
| 364 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 365 | void L2TPIPSecDriver::GetLogin(string *user, string *password) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 366 | LOG(INFO) << "Login requested."; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 367 | string user_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 368 | args()->LookupString(flimflam::kL2tpIpsecUserProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 369 | if (user_property.empty()) { |
| 370 | LOG(ERROR) << "User not set."; |
| 371 | return; |
| 372 | } |
| 373 | string password_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 374 | args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 375 | if (password_property.empty()) { |
| 376 | LOG(ERROR) << "Password not set."; |
| 377 | return; |
| 378 | } |
| 379 | *user = user_property; |
| 380 | *password = password_property; |
| 381 | } |
| 382 | |
| 383 | void L2TPIPSecDriver::Notify( |
| 384 | const string &reason, const map<string, string> &dict) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 385 | LOG(INFO) << "IP configuration received: " << reason; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 386 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 387 | if (reason != kPPPReasonConnect) { |
| 388 | DCHECK_EQ(kPPPReasonDisconnect, reason); |
| 389 | // DestroyLater, rather than while on stack. |
| 390 | external_task_.release()->DestroyLater(dispatcher()); |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 391 | FailService(Service::kFailureUnknown); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 395 | DeletePSKFile(); |
| 396 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 397 | string interface_name = PPPDevice::GetInterfaceName(dict); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 398 | int interface_index = device_info_->GetIndex(interface_name); |
| 399 | if (interface_index < 0) { |
| 400 | // TODO(petkov): Consider handling the race when the RTNL notification about |
| 401 | // the new PPP device has not been received yet. We can keep the IP |
Darin Petkov | f8f970a | 2012-09-03 11:32:55 +0200 | [diff] [blame] | 402 | // configuration and apply it when ClaimInterface is |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 403 | // invoked. crbug.com/212446. |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 404 | NOTIMPLEMENTED() << ": No device info for " << interface_name << "."; |
| 405 | return; |
| 406 | } |
| 407 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 408 | // There is no IPv6 support for L2TP/IPsec VPN at this moment, so create a |
| 409 | // blackhole route for IPv6 traffic after establishing a IPv4 VPN. |
| 410 | // TODO(benchan): Generalize this when IPv6 support is added. |
| 411 | bool blackhole_ipv6 = true; |
| 412 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 413 | if (!device_) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 414 | device_ = new PPPDevice(control_, dispatcher(), metrics_, manager(), |
| 415 | interface_name, interface_index); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 416 | } |
| 417 | device_->SetEnabled(true); |
| 418 | device_->SelectService(service_); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 419 | device_->UpdateIPConfigFromPPP(dict, blackhole_ipv6); |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 420 | ReportConnectionMetrics(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 421 | StopConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 424 | KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) { |
| 425 | SLOG(VPN, 2) << __func__; |
| 426 | KeyValueStore props = VPNDriver::GetProvider(error); |
| 427 | props.SetBool(flimflam::kPassphraseRequiredProperty, |
| 428 | args()->LookupString( |
| 429 | flimflam::kL2tpIpsecPasswordProperty, "").empty()); |
| 430 | props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty, |
| 431 | args()->LookupString( |
| 432 | flimflam::kL2tpIpsecPskProperty, "").empty()); |
| 433 | return props; |
| 434 | } |
| 435 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 436 | void L2TPIPSecDriver::ReportConnectionMetrics() { |
| 437 | metrics_->SendEnumToUMA( |
| 438 | Metrics::kMetricVpnDriver, |
| 439 | Metrics::kVpnDriverL2tpIpsec, |
| 440 | Metrics::kMetricVpnDriverMax); |
| 441 | |
| 442 | // We output an enum for each of the authentication types specified, |
| 443 | // even if more than one is set at the same time. |
| 444 | bool has_remote_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 445 | if (args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 446 | metrics_->SendEnumToUMA( |
| 447 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 448 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecCertificate, |
| 449 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 450 | has_remote_authentication = true; |
| 451 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 452 | if (args()->LookupString(flimflam::kL2tpIpsecPskProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 453 | metrics_->SendEnumToUMA( |
| 454 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 455 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecPsk, |
| 456 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 457 | has_remote_authentication = true; |
| 458 | } |
| 459 | if (!has_remote_authentication) { |
| 460 | metrics_->SendEnumToUMA( |
| 461 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 462 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecDefault, |
| 463 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 464 | } |
| 465 | |
| 466 | bool has_user_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 467 | if (args()->LookupString(flimflam::kL2tpIpsecClientCertIdProperty, |
| 468 | "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 469 | metrics_->SendEnumToUMA( |
| 470 | Metrics::kMetricVpnUserAuthenticationType, |
| 471 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecCertificate, |
| 472 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 473 | has_user_authentication = true; |
| 474 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 475 | if (args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 476 | metrics_->SendEnumToUMA( |
| 477 | Metrics::kMetricVpnUserAuthenticationType, |
| 478 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecUsernamePassword, |
| 479 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 480 | has_user_authentication = true; |
| 481 | } |
| 482 | if (!has_user_authentication) { |
| 483 | metrics_->SendEnumToUMA( |
| 484 | Metrics::kMetricVpnUserAuthenticationType, |
| 485 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecNone, |
| 486 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 487 | } |
| 488 | } |
| 489 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 490 | } // namespace shill |