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