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 | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 79 | { kL2tpIpsecCaCertPemProperty, 0 }, |
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 | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 104 | certificate_file_(new CertificateFile(glib)), |
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) { |
Darin Petkov | f8f970a | 2012-09-03 11:32:55 +0200 | [diff] [blame] | 113 | // TODO(petkov): crosbug.com/29970. |
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) { |
| 295 | string ca_cert = args()->LookupString(kL2tpIpsecCaCertPemProperty, ""); |
| 296 | if (ca_cert.empty()) { |
| 297 | return false; |
| 298 | } |
| 299 | FilePath certfile = certificate_file_->CreateDERFromString(ca_cert); |
| 300 | if (certfile.empty()) { |
| 301 | LOG(ERROR) << "Unable to extract certificate from PEM string."; |
| 302 | return false; |
| 303 | } |
| 304 | options->push_back("--server_ca_file"); |
| 305 | options->push_back(certfile.value()); |
| 306 | return true; |
| 307 | } |
| 308 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 309 | bool L2TPIPSecDriver::AppendValueOption( |
| 310 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 311 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 312 | if (!value.empty()) { |
| 313 | options->push_back(option); |
| 314 | options->push_back(value); |
| 315 | return true; |
| 316 | } |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | bool L2TPIPSecDriver::AppendFlag(const string &property, |
| 321 | const string &true_option, |
| 322 | const string &false_option, |
| 323 | vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 324 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 325 | if (!value.empty()) { |
| 326 | options->push_back(value == "true" ? true_option : false_option); |
| 327 | return true; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 332 | void L2TPIPSecDriver::OnL2TPIPSecVPNDied(pid_t /*pid*/, int status) { |
| 333 | FailService(TranslateExitStatusToFailure(status)); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 334 | // TODO(petkov): Figure if we need to restart the connection. |
| 335 | } |
| 336 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 337 | // static |
| 338 | Service::ConnectFailure L2TPIPSecDriver::TranslateExitStatusToFailure( |
| 339 | int status) { |
| 340 | if (!WIFEXITED(status)) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 341 | return Service::kFailureInternal; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 342 | } |
| 343 | switch (WEXITSTATUS(status)) { |
| 344 | case vpn_manager::kServiceErrorResolveHostnameFailed: |
| 345 | return Service::kFailureDNSLookup; |
| 346 | case vpn_manager::kServiceErrorIpsecConnectionFailed: |
| 347 | case vpn_manager::kServiceErrorL2tpConnectionFailed: |
| 348 | case vpn_manager::kServiceErrorPppConnectionFailed: |
| 349 | return Service::kFailureConnect; |
| 350 | case vpn_manager::kServiceErrorIpsecPresharedKeyAuthenticationFailed: |
| 351 | return Service::kFailureIPSecPSKAuth; |
| 352 | case vpn_manager::kServiceErrorIpsecCertificateAuthenticationFailed: |
| 353 | return Service::kFailureIPSecCertAuth; |
| 354 | case vpn_manager::kServiceErrorPppAuthenticationFailed: |
| 355 | return Service::kFailurePPPAuth; |
| 356 | default: |
| 357 | break; |
| 358 | } |
| 359 | return Service::kFailureUnknown; |
| 360 | } |
| 361 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 362 | void L2TPIPSecDriver::GetLogin(string *user, string *password) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 363 | LOG(INFO) << "Login requested."; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 364 | string user_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 365 | args()->LookupString(flimflam::kL2tpIpsecUserProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 366 | if (user_property.empty()) { |
| 367 | LOG(ERROR) << "User not set."; |
| 368 | return; |
| 369 | } |
| 370 | string password_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 371 | args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 372 | if (password_property.empty()) { |
| 373 | LOG(ERROR) << "Password not set."; |
| 374 | return; |
| 375 | } |
| 376 | *user = user_property; |
| 377 | *password = password_property; |
| 378 | } |
| 379 | |
| 380 | void L2TPIPSecDriver::Notify( |
| 381 | const string &reason, const map<string, string> &dict) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 382 | LOG(INFO) << "IP configuration received: " << reason; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 383 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 384 | if (reason != kPPPReasonConnect) { |
| 385 | DCHECK_EQ(kPPPReasonDisconnect, reason); |
| 386 | // DestroyLater, rather than while on stack. |
| 387 | external_task_.release()->DestroyLater(dispatcher()); |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 388 | FailService(Service::kFailureUnknown); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 392 | DeletePSKFile(); |
| 393 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 394 | string interface_name = PPPDevice::GetInterfaceName(dict); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 395 | int interface_index = device_info_->GetIndex(interface_name); |
| 396 | if (interface_index < 0) { |
| 397 | // TODO(petkov): Consider handling the race when the RTNL notification about |
| 398 | // 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] | 399 | // configuration and apply it when ClaimInterface is |
| 400 | // invoked. crosbug.com/29970. |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 401 | NOTIMPLEMENTED() << ": No device info for " << interface_name << "."; |
| 402 | return; |
| 403 | } |
| 404 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 405 | // There is no IPv6 support for L2TP/IPsec VPN at this moment, so create a |
| 406 | // blackhole route for IPv6 traffic after establishing a IPv4 VPN. |
| 407 | // TODO(benchan): Generalize this when IPv6 support is added. |
| 408 | bool blackhole_ipv6 = true; |
| 409 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 410 | if (!device_) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 411 | device_ = new PPPDevice(control_, dispatcher(), metrics_, manager(), |
| 412 | interface_name, interface_index); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 413 | } |
| 414 | device_->SetEnabled(true); |
| 415 | device_->SelectService(service_); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 416 | device_->UpdateIPConfigFromPPP(dict, blackhole_ipv6); |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 417 | ReportConnectionMetrics(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 418 | StopConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 421 | KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) { |
| 422 | SLOG(VPN, 2) << __func__; |
| 423 | KeyValueStore props = VPNDriver::GetProvider(error); |
| 424 | props.SetBool(flimflam::kPassphraseRequiredProperty, |
| 425 | args()->LookupString( |
| 426 | flimflam::kL2tpIpsecPasswordProperty, "").empty()); |
| 427 | props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty, |
| 428 | args()->LookupString( |
| 429 | flimflam::kL2tpIpsecPskProperty, "").empty()); |
| 430 | return props; |
| 431 | } |
| 432 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 433 | void L2TPIPSecDriver::ReportConnectionMetrics() { |
| 434 | metrics_->SendEnumToUMA( |
| 435 | Metrics::kMetricVpnDriver, |
| 436 | Metrics::kVpnDriverL2tpIpsec, |
| 437 | Metrics::kMetricVpnDriverMax); |
| 438 | |
| 439 | // We output an enum for each of the authentication types specified, |
| 440 | // even if more than one is set at the same time. |
| 441 | bool has_remote_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 442 | if (args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 443 | metrics_->SendEnumToUMA( |
| 444 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 445 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecCertificate, |
| 446 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 447 | has_remote_authentication = true; |
| 448 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 449 | if (args()->LookupString(flimflam::kL2tpIpsecPskProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 450 | metrics_->SendEnumToUMA( |
| 451 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 452 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecPsk, |
| 453 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 454 | has_remote_authentication = true; |
| 455 | } |
| 456 | if (!has_remote_authentication) { |
| 457 | metrics_->SendEnumToUMA( |
| 458 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 459 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecDefault, |
| 460 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 461 | } |
| 462 | |
| 463 | bool has_user_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 464 | if (args()->LookupString(flimflam::kL2tpIpsecClientCertIdProperty, |
| 465 | "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 466 | metrics_->SendEnumToUMA( |
| 467 | Metrics::kMetricVpnUserAuthenticationType, |
| 468 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecCertificate, |
| 469 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 470 | has_user_authentication = true; |
| 471 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 472 | if (args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 473 | metrics_->SendEnumToUMA( |
| 474 | Metrics::kMetricVpnUserAuthenticationType, |
| 475 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecUsernamePassword, |
| 476 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 477 | has_user_authentication = true; |
| 478 | } |
| 479 | if (!has_user_authentication) { |
| 480 | metrics_->SendEnumToUMA( |
| 481 | Metrics::kMetricVpnUserAuthenticationType, |
| 482 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecNone, |
| 483 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 484 | } |
| 485 | } |
| 486 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 487 | } // namespace shill |