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 | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 26 | #include <sys/wait.h> |
| 27 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 28 | #include <base/bind.h> |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 29 | #include <base/file_util.h> |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 30 | #include <base/string_util.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 31 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 32 | #include <chromeos/vpn-manager/service_error.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 33 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 34 | #include "shill/certificate_file.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 35 | #include "shill/device_info.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 36 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 37 | #include "shill/logging.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 38 | #include "shill/manager.h" |
| 39 | #include "shill/nss.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 40 | #include "shill/process_killer.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 41 | #include "shill/vpn.h" |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 42 | #include "shill/vpn_service.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 43 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 44 | using base::Bind; |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 45 | using base::Closure; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 46 | using base::FilePath; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 47 | using std::map; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 48 | using std::string; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 49 | using std::vector; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 50 | |
| 51 | namespace shill { |
| 52 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 53 | namespace { |
| 54 | const char kL2TPIPSecIPSecTimeoutProperty[] = "L2TPIPsec.IPsecTimeout"; |
| 55 | const char kL2TPIPSecLeftProtoPortProperty[] = "L2TPIPsec.LeftProtoPort"; |
| 56 | const char kL2TPIPSecLengthBitProperty[] = "L2TPIPsec.LengthBit"; |
| 57 | const char kL2TPIPSecPFSProperty[] = "L2TPIPsec.PFS"; |
| 58 | const char kL2TPIPSecRefusePapProperty[] = "L2TPIPsec.RefusePap"; |
| 59 | const char kL2TPIPSecRekeyProperty[] = "L2TPIPsec.Rekey"; |
| 60 | const char kL2TPIPSecRequireAuthProperty[] = "L2TPIPsec.RequireAuth"; |
| 61 | const char kL2TPIPSecRequireChapProperty[] = "L2TPIPsec.RequireChap"; |
| 62 | const char kL2TPIPSecRightProtoPortProperty[] = "L2TPIPsec.RightProtoPort"; |
| 63 | } // namespace |
| 64 | |
| 65 | // static |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 66 | const char L2TPIPSecDriver::kPPPDPlugin[] = SHIMDIR "/shill-pppd-plugin.so"; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 67 | // static |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 68 | const char L2TPIPSecDriver::kL2TPIPSecVPNPath[] = "/usr/sbin/l2tpipsec_vpn"; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 69 | // static |
| 70 | const VPNDriver::Property L2TPIPSecDriver::kProperties[] = { |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 71 | { flimflam::kL2tpIpsecAuthenticationType, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 72 | { flimflam::kL2tpIpsecCaCertNssProperty, 0 }, |
| 73 | { flimflam::kL2tpIpsecClientCertIdProperty, 0 }, |
| 74 | { flimflam::kL2tpIpsecClientCertSlotProperty, 0 }, |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 75 | { flimflam::kL2tpIpsecIkeVersion, 0 }, |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 76 | { flimflam::kL2tpIpsecPasswordProperty, |
| 77 | Property::kCredential | Property::kWriteOnly }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 78 | { flimflam::kL2tpIpsecPinProperty, Property::kCredential }, |
| 79 | { flimflam::kL2tpIpsecPskProperty, Property::kCredential }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 80 | { flimflam::kL2tpIpsecUserProperty, 0 }, |
Darin Petkov | 2c773c2 | 2012-04-26 12:54:11 +0200 | [diff] [blame] | 81 | { flimflam::kProviderHostProperty, 0 }, |
Darin Petkov | 2c773c2 | 2012-04-26 12:54:11 +0200 | [diff] [blame] | 82 | { flimflam::kProviderTypeProperty, 0 }, |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 83 | { kL2tpIpsecCaCertPemProperty, 0 }, |
Paul Stewart | 5bbf93b | 2013-03-21 12:06:08 -0700 | [diff] [blame] | 84 | { kL2tpIpsecTunnelGroupProperty, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 85 | { kL2TPIPSecIPSecTimeoutProperty, 0 }, |
| 86 | { kL2TPIPSecLeftProtoPortProperty, 0 }, |
| 87 | { kL2TPIPSecLengthBitProperty, 0 }, |
| 88 | { kL2TPIPSecPFSProperty, 0 }, |
| 89 | { kL2TPIPSecRefusePapProperty, 0 }, |
| 90 | { kL2TPIPSecRekeyProperty, 0 }, |
| 91 | { kL2TPIPSecRequireAuthProperty, 0 }, |
| 92 | { kL2TPIPSecRequireChapProperty, 0 }, |
| 93 | { kL2TPIPSecRightProtoPortProperty, 0 }, |
| 94 | }; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 95 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 96 | L2TPIPSecDriver::L2TPIPSecDriver(ControlInterface *control, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 97 | EventDispatcher *dispatcher, |
| 98 | Metrics *metrics, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 99 | Manager *manager, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 100 | DeviceInfo *device_info, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 101 | GLib *glib) |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 102 | : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)), |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 103 | control_(control), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 104 | metrics_(metrics), |
| 105 | device_info_(device_info), |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 106 | glib_(glib), |
| 107 | nss_(NSS::GetInstance()), |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 108 | process_killer_(ProcessKiller::GetInstance()), |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 109 | certificate_file_(new CertificateFile(glib)), |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 110 | pid_(0), |
| 111 | child_watch_tag_(0) {} |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 112 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 113 | L2TPIPSecDriver::~L2TPIPSecDriver() { |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 114 | IdleService(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 115 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 116 | |
| 117 | bool L2TPIPSecDriver::ClaimInterface(const string &link_name, |
| 118 | int interface_index) { |
Darin Petkov | f8f970a | 2012-09-03 11:32:55 +0200 | [diff] [blame] | 119 | // TODO(petkov): crosbug.com/29970. |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 120 | NOTIMPLEMENTED(); |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | void L2TPIPSecDriver::Connect(const VPNServiceRefPtr &service, Error *error) { |
Darin Petkov | 0cd0d1e | 2013-02-11 12:49:10 +0100 | [diff] [blame] | 125 | StartConnectTimeout(kDefaultConnectTimeoutSeconds); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 126 | service_ = service; |
| 127 | service_->SetState(Service::kStateConfiguring); |
| 128 | rpc_task_.reset(new RPCTask(control_, this)); |
| 129 | if (!SpawnL2TPIPSecVPN(error)) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 130 | FailService(Service::kFailureInternal); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 131 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void L2TPIPSecDriver::Disconnect() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 135 | SLOG(VPN, 2) << __func__; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 136 | IdleService(); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 139 | void L2TPIPSecDriver::OnConnectionDisconnected() { |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 140 | LOG(INFO) << "Underlying connection disconnected."; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 141 | IdleService(); |
Darin Petkov | a42afe3 | 2013-02-05 16:53:52 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void L2TPIPSecDriver::OnConnectTimeout() { |
| 145 | VPNDriver::OnConnectTimeout(); |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 146 | FailService(Service::kFailureConnect); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 149 | string L2TPIPSecDriver::GetProviderType() const { |
| 150 | return flimflam::kProviderL2tpIpsec; |
| 151 | } |
| 152 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 153 | void L2TPIPSecDriver::IdleService() { |
| 154 | Cleanup(Service::kStateIdle, Service::kFailureUnknown); |
| 155 | } |
| 156 | |
| 157 | void L2TPIPSecDriver::FailService(Service::ConnectFailure failure) { |
| 158 | Cleanup(Service::kStateFailure, failure); |
| 159 | } |
| 160 | |
| 161 | void L2TPIPSecDriver::Cleanup(Service::ConnectState state, |
| 162 | Service::ConnectFailure failure) { |
| 163 | SLOG(VPN, 2) << __func__ << "(" |
| 164 | << Service::ConnectStateToString(state) << ", " |
| 165 | << Service::ConnectFailureToString(failure) << ")"; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 166 | StopConnectTimeout(); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 167 | DeletePSKFile(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 168 | if (child_watch_tag_) { |
| 169 | glib_->SourceRemove(child_watch_tag_); |
| 170 | child_watch_tag_ = 0; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 171 | } |
| 172 | if (pid_) { |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 173 | process_killer_->Kill(pid_, Closure()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 174 | pid_ = 0; |
| 175 | } |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 176 | if (device_) { |
| 177 | device_->OnDisconnected(); |
| 178 | device_->SetEnabled(false); |
| 179 | device_ = NULL; |
| 180 | } |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 181 | rpc_task_.reset(); |
| 182 | if (service_) { |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 183 | if (state == Service::kStateFailure) { |
| 184 | service_->SetFailure(failure); |
| 185 | } else { |
| 186 | service_->SetState(state); |
| 187 | } |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 188 | service_ = NULL; |
| 189 | } |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 192 | void L2TPIPSecDriver::DeletePSKFile() { |
| 193 | if (!psk_file_.empty()) { |
| 194 | file_util::Delete(psk_file_, false); |
| 195 | psk_file_.clear(); |
| 196 | } |
| 197 | } |
| 198 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 199 | bool L2TPIPSecDriver::SpawnL2TPIPSecVPN(Error *error) { |
| 200 | SLOG(VPN, 2) << __func__; |
| 201 | |
| 202 | vector<string> options; |
| 203 | if (!InitOptions(&options, error)) { |
| 204 | return false; |
| 205 | } |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 206 | LOG(INFO) << "L2TP/IPSec VPN process options: " << JoinString(options, ' '); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 207 | |
| 208 | // TODO(petkov): This code needs to be abstracted away in a separate external |
| 209 | // process module (crosbug.com/27131). |
| 210 | vector<char *> process_args; |
| 211 | process_args.push_back(const_cast<char *>(kL2TPIPSecVPNPath)); |
| 212 | for (vector<string>::const_iterator it = options.begin(); |
| 213 | it != options.end(); ++it) { |
| 214 | process_args.push_back(const_cast<char *>(it->c_str())); |
| 215 | } |
| 216 | process_args.push_back(NULL); |
| 217 | |
| 218 | vector<string> environment; |
| 219 | InitEnvironment(&environment); |
| 220 | |
| 221 | vector<char *> process_env; |
| 222 | for (vector<string>::const_iterator it = environment.begin(); |
| 223 | it != environment.end(); ++it) { |
| 224 | process_env.push_back(const_cast<char *>(it->c_str())); |
| 225 | } |
| 226 | process_env.push_back(NULL); |
| 227 | |
| 228 | CHECK(!pid_); |
Darin Petkov | 68710d7 | 2013-02-13 14:22:56 +0100 | [diff] [blame] | 229 | if (!glib_->SpawnAsync(NULL, |
| 230 | process_args.data(), |
| 231 | process_env.data(), |
| 232 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 233 | NULL, |
| 234 | NULL, |
| 235 | &pid_, |
| 236 | NULL)) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 237 | Error::PopulateAndLog(error, Error::kInternalError, |
| 238 | string("Unable to spawn: ") + process_args[0]); |
| 239 | return false; |
| 240 | } |
| 241 | CHECK(!child_watch_tag_); |
| 242 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, OnL2TPIPSecVPNDied, this); |
| 243 | return true; |
| 244 | } |
| 245 | |
| 246 | void L2TPIPSecDriver::InitEnvironment(vector<string> *environment) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 247 | environment->push_back(string(kRPCTaskServiceVariable) + "=" + |
| 248 | rpc_task_->GetRpcConnectionIdentifier()); |
| 249 | environment->push_back(string(kRPCTaskPathVariable) + "=" + |
| 250 | rpc_task_->GetRpcIdentifier()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | bool L2TPIPSecDriver::InitOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 254 | string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 255 | if (vpnhost.empty()) { |
| 256 | Error::PopulateAndLog( |
| 257 | error, Error::kInvalidArguments, "VPN host not specified."); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 258 | return false; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (!InitPSKOptions(options, error)) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 262 | return false; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | options->push_back("--remote_host"); |
| 266 | options->push_back(vpnhost); |
| 267 | options->push_back("--pppd_plugin"); |
| 268 | options->push_back(kPPPDPlugin); |
| 269 | // Disable pppd from configuring IP addresses, routes, DNS. |
| 270 | options->push_back("--nosystemconfig"); |
| 271 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 272 | // Accept a PEM CA certificate or an NSS certificate, but not both. |
| 273 | // Prefer PEM to NSS. |
| 274 | if (!InitPEMOptions(options)) { |
| 275 | InitNSSOptions(options); |
| 276 | } |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 277 | |
| 278 | AppendValueOption(flimflam::kL2tpIpsecClientCertIdProperty, |
| 279 | "--client_cert_id", options); |
| 280 | AppendValueOption(flimflam::kL2tpIpsecClientCertSlotProperty, |
| 281 | "--client_cert_slot", options); |
| 282 | AppendValueOption(flimflam::kL2tpIpsecPinProperty, "--user_pin", options); |
| 283 | AppendValueOption(flimflam::kL2tpIpsecUserProperty, "--user", options); |
| 284 | AppendValueOption(kL2TPIPSecIPSecTimeoutProperty, "--ipsec_timeout", options); |
| 285 | AppendValueOption(kL2TPIPSecLeftProtoPortProperty, |
| 286 | "--leftprotoport", options); |
| 287 | AppendFlag(kL2TPIPSecPFSProperty, "--pfs", "--nopfs", options); |
| 288 | AppendFlag(kL2TPIPSecRekeyProperty, "--rekey", "--norekey", options); |
| 289 | AppendValueOption(kL2TPIPSecRightProtoPortProperty, |
| 290 | "--rightprotoport", options); |
| 291 | AppendFlag(kL2TPIPSecRequireChapProperty, |
| 292 | "--require_chap", "--norequire_chap", options); |
| 293 | AppendFlag(kL2TPIPSecRefusePapProperty, |
| 294 | "--refuse_pap", "--norefuse_pap", options); |
| 295 | AppendFlag(kL2TPIPSecRequireAuthProperty, |
| 296 | "--require_authentication", "--norequire_authentication", options); |
| 297 | AppendFlag(kL2TPIPSecLengthBitProperty, |
| 298 | "--length_bit", "--nolength_bit", options); |
Paul Stewart | 5bbf93b | 2013-03-21 12:06:08 -0700 | [diff] [blame] | 299 | AppendValueOption(kL2tpIpsecTunnelGroupProperty, "--tunnel_group", options); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 300 | if (SLOG_IS_ON(VPN, 0)) { |
| 301 | options->push_back("--debug"); |
| 302 | } |
| 303 | return true; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | bool L2TPIPSecDriver::InitPSKOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 307 | string psk = args()->LookupString(flimflam::kL2tpIpsecPskProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 308 | if (!psk.empty()) { |
| 309 | if (!file_util::CreateTemporaryFileInDir( |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 310 | manager()->run_path(), &psk_file_) || |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 311 | chmod(psk_file_.value().c_str(), S_IRUSR | S_IWUSR) || |
| 312 | file_util::WriteFile(psk_file_, psk.data(), psk.size()) != |
| 313 | static_cast<int>(psk.size())) { |
| 314 | Error::PopulateAndLog( |
| 315 | error, Error::kInternalError, "Unable to setup psk file."); |
| 316 | return false; |
| 317 | } |
| 318 | options->push_back("--psk_file"); |
| 319 | options->push_back(psk_file_.value()); |
| 320 | } |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | void L2TPIPSecDriver::InitNSSOptions(vector<string> *options) { |
| 325 | string ca_cert = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 326 | args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 327 | if (!ca_cert.empty()) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 328 | const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 329 | vector<char> id(vpnhost.begin(), vpnhost.end()); |
| 330 | FilePath certfile = nss_->GetDERCertfile(ca_cert, id); |
| 331 | if (certfile.empty()) { |
| 332 | LOG(ERROR) << "Unable to extract certificate: " << ca_cert; |
| 333 | } else { |
| 334 | options->push_back("--server_ca_file"); |
| 335 | options->push_back(certfile.value()); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
Paul Stewart | 5baebb7 | 2013-03-14 11:43:29 -0700 | [diff] [blame] | 340 | bool L2TPIPSecDriver::InitPEMOptions(vector<string> *options) { |
| 341 | string ca_cert = args()->LookupString(kL2tpIpsecCaCertPemProperty, ""); |
| 342 | if (ca_cert.empty()) { |
| 343 | return false; |
| 344 | } |
| 345 | FilePath certfile = certificate_file_->CreateDERFromString(ca_cert); |
| 346 | if (certfile.empty()) { |
| 347 | LOG(ERROR) << "Unable to extract certificate from PEM string."; |
| 348 | return false; |
| 349 | } |
| 350 | options->push_back("--server_ca_file"); |
| 351 | options->push_back(certfile.value()); |
| 352 | return true; |
| 353 | } |
| 354 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 355 | bool L2TPIPSecDriver::AppendValueOption( |
| 356 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 357 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 358 | if (!value.empty()) { |
| 359 | options->push_back(option); |
| 360 | options->push_back(value); |
| 361 | return true; |
| 362 | } |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | bool L2TPIPSecDriver::AppendFlag(const string &property, |
| 367 | const string &true_option, |
| 368 | const string &false_option, |
| 369 | vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 370 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 371 | if (!value.empty()) { |
| 372 | options->push_back(value == "true" ? true_option : false_option); |
| 373 | return true; |
| 374 | } |
| 375 | return false; |
| 376 | } |
| 377 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 378 | // static |
| 379 | void L2TPIPSecDriver::OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data) { |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 380 | LOG(INFO) << __func__ << "(" << pid << ", " << status << ")"; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 381 | L2TPIPSecDriver *me = reinterpret_cast<L2TPIPSecDriver *>(data); |
| 382 | me->child_watch_tag_ = 0; |
| 383 | CHECK_EQ(pid, me->pid_); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 384 | me->pid_ = 0; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 385 | me->FailService(TranslateExitStatusToFailure(status)); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 386 | // TODO(petkov): Figure if we need to restart the connection. |
| 387 | } |
| 388 | |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 389 | // static |
| 390 | Service::ConnectFailure L2TPIPSecDriver::TranslateExitStatusToFailure( |
| 391 | int status) { |
| 392 | if (!WIFEXITED(status)) { |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 393 | return Service::kFailureInternal; |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 394 | } |
| 395 | switch (WEXITSTATUS(status)) { |
| 396 | case vpn_manager::kServiceErrorResolveHostnameFailed: |
| 397 | return Service::kFailureDNSLookup; |
| 398 | case vpn_manager::kServiceErrorIpsecConnectionFailed: |
| 399 | case vpn_manager::kServiceErrorL2tpConnectionFailed: |
| 400 | case vpn_manager::kServiceErrorPppConnectionFailed: |
| 401 | return Service::kFailureConnect; |
| 402 | case vpn_manager::kServiceErrorIpsecPresharedKeyAuthenticationFailed: |
| 403 | return Service::kFailureIPSecPSKAuth; |
| 404 | case vpn_manager::kServiceErrorIpsecCertificateAuthenticationFailed: |
| 405 | return Service::kFailureIPSecCertAuth; |
| 406 | case vpn_manager::kServiceErrorPppAuthenticationFailed: |
| 407 | return Service::kFailurePPPAuth; |
| 408 | default: |
| 409 | break; |
| 410 | } |
| 411 | return Service::kFailureUnknown; |
| 412 | } |
| 413 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 414 | void L2TPIPSecDriver::GetLogin(string *user, string *password) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 415 | LOG(INFO) << "Login requested."; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 416 | string user_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 417 | args()->LookupString(flimflam::kL2tpIpsecUserProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 418 | if (user_property.empty()) { |
| 419 | LOG(ERROR) << "User not set."; |
| 420 | return; |
| 421 | } |
| 422 | string password_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 423 | args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 424 | if (password_property.empty()) { |
| 425 | LOG(ERROR) << "Password not set."; |
| 426 | return; |
| 427 | } |
| 428 | *user = user_property; |
| 429 | *password = password_property; |
| 430 | } |
| 431 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 432 | void L2TPIPSecDriver::ParseIPConfiguration( |
| 433 | const map<string, string> &configuration, |
| 434 | IPConfig::Properties *properties, |
| 435 | string *interface_name) { |
| 436 | properties->address_family = IPAddress::kFamilyIPv4; |
| 437 | properties->subnet_prefix = IPAddress::GetMaxPrefixLength( |
| 438 | properties->address_family); |
| 439 | for (map<string, string>::const_iterator it = configuration.begin(); |
| 440 | it != configuration.end(); ++it) { |
| 441 | const string &key = it->first; |
| 442 | const string &value = it->second; |
| 443 | SLOG(VPN, 2) << "Processing: " << key << " -> " << value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 444 | if (key == kL2TPIPSecInternalIP4Address) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 445 | properties->address = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 446 | } else if (key == kL2TPIPSecExternalIP4Address) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 447 | properties->peer_address = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 448 | } else if (key == kL2TPIPSecGatewayAddress) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 449 | properties->gateway = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 450 | } else if (key == kL2TPIPSecDNS1) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 451 | properties->dns_servers.insert(properties->dns_servers.begin(), value); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 452 | } else if (key == kL2TPIPSecDNS2) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 453 | properties->dns_servers.push_back(value); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 454 | } else if (key == kL2TPIPSecInterfaceName) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 455 | *interface_name = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 456 | } else if (key == kL2TPIPSecLNSAddress) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 457 | properties->trusted_ip = value; |
| 458 | } else { |
| 459 | SLOG(VPN, 2) << "Key ignored."; |
| 460 | } |
| 461 | } |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 462 | |
| 463 | // There is no IPv6 support for L2TP/IPsec VPN at this moment, so create a |
| 464 | // blackhole route for IPv6 traffic after establishing a IPv4 VPN. |
| 465 | // TODO(benchan): Generalize this when IPv6 support is added. |
| 466 | properties->blackhole_ipv6 = true; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 469 | void L2TPIPSecDriver::Notify( |
| 470 | const string &reason, const map<string, string> &dict) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 471 | LOG(INFO) << "IP configuration received: " << reason; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 472 | |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 473 | if (reason != kL2TPIPSecReasonConnect) { |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 474 | DCHECK(reason == kL2TPIPSecReasonDisconnect); |
| 475 | // Avoid destroying the RPC task inside the adaptor callback by doing it |
| 476 | // from the main event loop. |
| 477 | dispatcher()->PostTask(Bind(&DeleteRPCTask, rpc_task_.release())); |
Darin Petkov | 85d5317 | 2013-03-13 16:43:28 +0100 | [diff] [blame] | 478 | FailService(Service::kFailureUnknown); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 482 | DeletePSKFile(); |
| 483 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 484 | IPConfig::Properties properties; |
| 485 | string interface_name; |
| 486 | ParseIPConfiguration(dict, &properties, &interface_name); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 487 | |
| 488 | int interface_index = device_info_->GetIndex(interface_name); |
| 489 | if (interface_index < 0) { |
| 490 | // TODO(petkov): Consider handling the race when the RTNL notification about |
| 491 | // 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] | 492 | // configuration and apply it when ClaimInterface is |
| 493 | // invoked. crosbug.com/29970. |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 494 | NOTIMPLEMENTED() << ": No device info for " << interface_name << "."; |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | if (!device_) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 499 | device_ = new VPN(control_, dispatcher(), metrics_, manager(), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 500 | interface_name, interface_index); |
| 501 | } |
| 502 | device_->SetEnabled(true); |
| 503 | device_->SelectService(service_); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 504 | device_->UpdateIPConfig(properties); |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 505 | ReportConnectionMetrics(); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 506 | StopConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Darin Petkov | 6999022 | 2012-11-14 09:25:25 +0100 | [diff] [blame] | 509 | // static |
| 510 | void L2TPIPSecDriver::DeleteRPCTask(RPCTask *rpc_task) { |
| 511 | delete rpc_task; |
| 512 | } |
| 513 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 514 | KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) { |
| 515 | SLOG(VPN, 2) << __func__; |
| 516 | KeyValueStore props = VPNDriver::GetProvider(error); |
| 517 | props.SetBool(flimflam::kPassphraseRequiredProperty, |
| 518 | args()->LookupString( |
| 519 | flimflam::kL2tpIpsecPasswordProperty, "").empty()); |
| 520 | props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty, |
| 521 | args()->LookupString( |
| 522 | flimflam::kL2tpIpsecPskProperty, "").empty()); |
| 523 | return props; |
| 524 | } |
| 525 | |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 526 | void L2TPIPSecDriver::ReportConnectionMetrics() { |
| 527 | metrics_->SendEnumToUMA( |
| 528 | Metrics::kMetricVpnDriver, |
| 529 | Metrics::kVpnDriverL2tpIpsec, |
| 530 | Metrics::kMetricVpnDriverMax); |
| 531 | |
| 532 | // We output an enum for each of the authentication types specified, |
| 533 | // even if more than one is set at the same time. |
| 534 | bool has_remote_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 535 | if (args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 536 | metrics_->SendEnumToUMA( |
| 537 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 538 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecCertificate, |
| 539 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 540 | has_remote_authentication = true; |
| 541 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 542 | if (args()->LookupString(flimflam::kL2tpIpsecPskProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 543 | metrics_->SendEnumToUMA( |
| 544 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 545 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecPsk, |
| 546 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 547 | has_remote_authentication = true; |
| 548 | } |
| 549 | if (!has_remote_authentication) { |
| 550 | metrics_->SendEnumToUMA( |
| 551 | Metrics::kMetricVpnRemoteAuthenticationType, |
| 552 | Metrics::kVpnRemoteAuthenticationTypeL2tpIpsecDefault, |
| 553 | Metrics::kMetricVpnRemoteAuthenticationTypeMax); |
| 554 | } |
| 555 | |
| 556 | bool has_user_authentication = false; |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 557 | if (args()->LookupString(flimflam::kL2tpIpsecClientCertIdProperty, |
| 558 | "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 559 | metrics_->SendEnumToUMA( |
| 560 | Metrics::kMetricVpnUserAuthenticationType, |
| 561 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecCertificate, |
| 562 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 563 | has_user_authentication = true; |
| 564 | } |
Paul Stewart | e8e71da | 2013-03-20 08:48:33 -0700 | [diff] [blame] | 565 | if (args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, "") != "") { |
Paul Stewart | 91a43cb | 2013-03-02 21:34:15 -0800 | [diff] [blame] | 566 | metrics_->SendEnumToUMA( |
| 567 | Metrics::kMetricVpnUserAuthenticationType, |
| 568 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecUsernamePassword, |
| 569 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 570 | has_user_authentication = true; |
| 571 | } |
| 572 | if (!has_user_authentication) { |
| 573 | metrics_->SendEnumToUMA( |
| 574 | Metrics::kMetricVpnUserAuthenticationType, |
| 575 | Metrics::kVpnUserAuthenticationTypeL2tpIpsecNone, |
| 576 | Metrics::kMetricVpnUserAuthenticationTypeMax); |
| 577 | } |
| 578 | } |
| 579 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 580 | } // namespace shill |