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 | |
| 5 | #include "shill/l2tp_ipsec_driver.h" |
| 6 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 7 | #include <base/file_util.h> |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 8 | #include <base/string_util.h> |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 9 | #include <chromeos/dbus/service_constants.h> |
| 10 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 11 | #include "shill/device_info.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 12 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 14 | #include "shill/manager.h" |
| 15 | #include "shill/nss.h" |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 16 | #include "shill/process_killer.h" |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 17 | #include "shill/vpn.h" |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 18 | #include "shill/vpn_service.h" |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 19 | |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 20 | using base::Closure; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 21 | using std::map; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 22 | using std::string; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 23 | using std::vector; |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 24 | |
| 25 | namespace shill { |
| 26 | |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 27 | namespace { |
| 28 | const char kL2TPIPSecIPSecTimeoutProperty[] = "L2TPIPsec.IPsecTimeout"; |
| 29 | const char kL2TPIPSecLeftProtoPortProperty[] = "L2TPIPsec.LeftProtoPort"; |
| 30 | const char kL2TPIPSecLengthBitProperty[] = "L2TPIPsec.LengthBit"; |
| 31 | const char kL2TPIPSecPFSProperty[] = "L2TPIPsec.PFS"; |
| 32 | const char kL2TPIPSecRefusePapProperty[] = "L2TPIPsec.RefusePap"; |
| 33 | const char kL2TPIPSecRekeyProperty[] = "L2TPIPsec.Rekey"; |
| 34 | const char kL2TPIPSecRequireAuthProperty[] = "L2TPIPsec.RequireAuth"; |
| 35 | const char kL2TPIPSecRequireChapProperty[] = "L2TPIPsec.RequireChap"; |
| 36 | const char kL2TPIPSecRightProtoPortProperty[] = "L2TPIPsec.RightProtoPort"; |
| 37 | } // namespace |
| 38 | |
| 39 | // static |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 40 | const char L2TPIPSecDriver::kPPPDPlugin[] = SHIMDIR "/shill-pppd-plugin.so"; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 41 | // static |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 42 | const char L2TPIPSecDriver::kL2TPIPSecVPNPath[] = "/usr/sbin/l2tpipsec_vpn"; |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 43 | // static |
| 44 | const VPNDriver::Property L2TPIPSecDriver::kProperties[] = { |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 45 | { flimflam::kL2tpIpsecAuthenticationType, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 46 | { flimflam::kL2tpIpsecCaCertNssProperty, 0 }, |
| 47 | { flimflam::kL2tpIpsecClientCertIdProperty, 0 }, |
| 48 | { flimflam::kL2tpIpsecClientCertSlotProperty, 0 }, |
Darin Petkov | a56bf97 | 2012-10-09 12:27:32 +0200 | [diff] [blame] | 49 | { flimflam::kL2tpIpsecIkeVersion, 0 }, |
Darin Petkov | 0223655 | 2012-06-11 13:15:19 +0200 | [diff] [blame] | 50 | { flimflam::kL2tpIpsecPasswordProperty, |
| 51 | Property::kCredential | Property::kWriteOnly }, |
Darin Petkov | cb71529 | 2012-04-25 13:04:37 +0200 | [diff] [blame] | 52 | { flimflam::kL2tpIpsecPinProperty, Property::kCredential }, |
| 53 | { flimflam::kL2tpIpsecPskProperty, Property::kCredential }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 54 | { flimflam::kL2tpIpsecUserProperty, 0 }, |
Darin Petkov | 2c773c2 | 2012-04-26 12:54:11 +0200 | [diff] [blame] | 55 | { flimflam::kProviderHostProperty, 0 }, |
| 56 | { flimflam::kProviderNameProperty, 0 }, |
| 57 | { flimflam::kProviderTypeProperty, 0 }, |
Darin Petkov | d432539 | 2012-04-23 15:48:22 +0200 | [diff] [blame] | 58 | { kL2TPIPSecIPSecTimeoutProperty, 0 }, |
| 59 | { kL2TPIPSecLeftProtoPortProperty, 0 }, |
| 60 | { kL2TPIPSecLengthBitProperty, 0 }, |
| 61 | { kL2TPIPSecPFSProperty, 0 }, |
| 62 | { kL2TPIPSecRefusePapProperty, 0 }, |
| 63 | { kL2TPIPSecRekeyProperty, 0 }, |
| 64 | { kL2TPIPSecRequireAuthProperty, 0 }, |
| 65 | { kL2TPIPSecRequireChapProperty, 0 }, |
| 66 | { kL2TPIPSecRightProtoPortProperty, 0 }, |
| 67 | }; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 68 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 69 | L2TPIPSecDriver::L2TPIPSecDriver(ControlInterface *control, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 70 | EventDispatcher *dispatcher, |
| 71 | Metrics *metrics, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 72 | Manager *manager, |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 73 | DeviceInfo *device_info, |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 74 | GLib *glib) |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 75 | : VPNDriver(dispatcher, manager, kProperties, arraysize(kProperties)), |
Darin Petkov | b451d6e | 2012-04-23 11:56:41 +0200 | [diff] [blame] | 76 | control_(control), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 77 | metrics_(metrics), |
| 78 | device_info_(device_info), |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 79 | glib_(glib), |
| 80 | nss_(NSS::GetInstance()), |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 81 | process_killer_(ProcessKiller::GetInstance()), |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 82 | pid_(0), |
| 83 | child_watch_tag_(0) {} |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 84 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 85 | L2TPIPSecDriver::~L2TPIPSecDriver() { |
| 86 | Cleanup(Service::kStateIdle); |
| 87 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 88 | |
| 89 | bool L2TPIPSecDriver::ClaimInterface(const string &link_name, |
| 90 | int interface_index) { |
Darin Petkov | f8f970a | 2012-09-03 11:32:55 +0200 | [diff] [blame] | 91 | // TODO(petkov): crosbug.com/29970. |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 92 | NOTIMPLEMENTED(); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | void L2TPIPSecDriver::Connect(const VPNServiceRefPtr &service, Error *error) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 97 | StartConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 98 | service_ = service; |
| 99 | service_->SetState(Service::kStateConfiguring); |
| 100 | rpc_task_.reset(new RPCTask(control_, this)); |
| 101 | if (!SpawnL2TPIPSecVPN(error)) { |
| 102 | Cleanup(Service::kStateFailure); |
| 103 | } |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void L2TPIPSecDriver::Disconnect() { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 107 | SLOG(VPN, 2) << __func__; |
| 108 | Cleanup(Service::kStateIdle); |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 111 | void L2TPIPSecDriver::OnConnectionDisconnected() { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 112 | LOG(ERROR) << "VPN connection disconnected."; |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 113 | Cleanup(Service::kStateFailure); |
| 114 | } |
| 115 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 116 | string L2TPIPSecDriver::GetProviderType() const { |
| 117 | return flimflam::kProviderL2tpIpsec; |
| 118 | } |
| 119 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 120 | void L2TPIPSecDriver::Cleanup(Service::ConnectState state) { |
| 121 | SLOG(VPN, 2) << __func__ |
| 122 | << "(" << Service::ConnectStateToString(state) << ")"; |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 123 | StopConnectTimeout(); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 124 | DeletePSKFile(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 125 | if (child_watch_tag_) { |
| 126 | glib_->SourceRemove(child_watch_tag_); |
| 127 | child_watch_tag_ = 0; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 128 | } |
| 129 | if (pid_) { |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 130 | process_killer_->Kill(pid_, Closure()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 131 | pid_ = 0; |
| 132 | } |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 133 | if (device_) { |
| 134 | device_->OnDisconnected(); |
| 135 | device_->SetEnabled(false); |
| 136 | device_ = NULL; |
| 137 | } |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 138 | rpc_task_.reset(); |
| 139 | if (service_) { |
| 140 | service_->SetState(state); |
| 141 | service_ = NULL; |
| 142 | } |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 145 | void L2TPIPSecDriver::DeletePSKFile() { |
| 146 | if (!psk_file_.empty()) { |
| 147 | file_util::Delete(psk_file_, false); |
| 148 | psk_file_.clear(); |
| 149 | } |
| 150 | } |
| 151 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 152 | bool L2TPIPSecDriver::SpawnL2TPIPSecVPN(Error *error) { |
| 153 | SLOG(VPN, 2) << __func__; |
| 154 | |
| 155 | vector<string> options; |
| 156 | if (!InitOptions(&options, error)) { |
| 157 | return false; |
| 158 | } |
| 159 | SLOG(VPN, 2) << "L2TP/IPSec VPN process options: " |
| 160 | << JoinString(options, ' '); |
| 161 | |
| 162 | // TODO(petkov): This code needs to be abstracted away in a separate external |
| 163 | // process module (crosbug.com/27131). |
| 164 | vector<char *> process_args; |
| 165 | process_args.push_back(const_cast<char *>(kL2TPIPSecVPNPath)); |
| 166 | for (vector<string>::const_iterator it = options.begin(); |
| 167 | it != options.end(); ++it) { |
| 168 | process_args.push_back(const_cast<char *>(it->c_str())); |
| 169 | } |
| 170 | process_args.push_back(NULL); |
| 171 | |
| 172 | vector<string> environment; |
| 173 | InitEnvironment(&environment); |
| 174 | |
| 175 | vector<char *> process_env; |
| 176 | for (vector<string>::const_iterator it = environment.begin(); |
| 177 | it != environment.end(); ++it) { |
| 178 | process_env.push_back(const_cast<char *>(it->c_str())); |
| 179 | } |
| 180 | process_env.push_back(NULL); |
| 181 | |
| 182 | CHECK(!pid_); |
| 183 | // Redirect all l2tp/ipsec output to stderr. |
| 184 | int stderr_fd = fileno(stderr); |
| 185 | if (!glib_->SpawnAsyncWithPipesCWD(process_args.data(), |
| 186 | process_env.data(), |
| 187 | G_SPAWN_DO_NOT_REAP_CHILD, |
| 188 | NULL, |
| 189 | NULL, |
| 190 | &pid_, |
| 191 | NULL, |
| 192 | &stderr_fd, |
| 193 | &stderr_fd, |
| 194 | NULL)) { |
| 195 | Error::PopulateAndLog(error, Error::kInternalError, |
| 196 | string("Unable to spawn: ") + process_args[0]); |
| 197 | return false; |
| 198 | } |
| 199 | CHECK(!child_watch_tag_); |
| 200 | child_watch_tag_ = glib_->ChildWatchAdd(pid_, OnL2TPIPSecVPNDied, this); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | void L2TPIPSecDriver::InitEnvironment(vector<string> *environment) { |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 205 | environment->push_back(string(kRPCTaskServiceVariable) + "=" + |
| 206 | rpc_task_->GetRpcConnectionIdentifier()); |
| 207 | environment->push_back(string(kRPCTaskPathVariable) + "=" + |
| 208 | rpc_task_->GetRpcIdentifier()); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool L2TPIPSecDriver::InitOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 212 | string vpnhost = args()->LookupString(flimflam::kProviderHostProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 213 | if (vpnhost.empty()) { |
| 214 | Error::PopulateAndLog( |
| 215 | error, Error::kInvalidArguments, "VPN host not specified."); |
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 | if (!InitPSKOptions(options, error)) { |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 220 | return false; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | options->push_back("--remote_host"); |
| 224 | options->push_back(vpnhost); |
| 225 | options->push_back("--pppd_plugin"); |
| 226 | options->push_back(kPPPDPlugin); |
| 227 | // Disable pppd from configuring IP addresses, routes, DNS. |
| 228 | options->push_back("--nosystemconfig"); |
| 229 | |
| 230 | InitNSSOptions(options); |
| 231 | |
| 232 | AppendValueOption(flimflam::kL2tpIpsecClientCertIdProperty, |
| 233 | "--client_cert_id", options); |
| 234 | AppendValueOption(flimflam::kL2tpIpsecClientCertSlotProperty, |
| 235 | "--client_cert_slot", options); |
| 236 | AppendValueOption(flimflam::kL2tpIpsecPinProperty, "--user_pin", options); |
| 237 | AppendValueOption(flimflam::kL2tpIpsecUserProperty, "--user", options); |
| 238 | AppendValueOption(kL2TPIPSecIPSecTimeoutProperty, "--ipsec_timeout", options); |
| 239 | AppendValueOption(kL2TPIPSecLeftProtoPortProperty, |
| 240 | "--leftprotoport", options); |
| 241 | AppendFlag(kL2TPIPSecPFSProperty, "--pfs", "--nopfs", options); |
| 242 | AppendFlag(kL2TPIPSecRekeyProperty, "--rekey", "--norekey", options); |
| 243 | AppendValueOption(kL2TPIPSecRightProtoPortProperty, |
| 244 | "--rightprotoport", options); |
| 245 | AppendFlag(kL2TPIPSecRequireChapProperty, |
| 246 | "--require_chap", "--norequire_chap", options); |
| 247 | AppendFlag(kL2TPIPSecRefusePapProperty, |
| 248 | "--refuse_pap", "--norefuse_pap", options); |
| 249 | AppendFlag(kL2TPIPSecRequireAuthProperty, |
| 250 | "--require_authentication", "--norequire_authentication", options); |
| 251 | AppendFlag(kL2TPIPSecLengthBitProperty, |
| 252 | "--length_bit", "--nolength_bit", options); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 253 | if (SLOG_IS_ON(VPN, 0)) { |
| 254 | options->push_back("--debug"); |
| 255 | } |
| 256 | return true; |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool L2TPIPSecDriver::InitPSKOptions(vector<string> *options, Error *error) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 260 | string psk = args()->LookupString(flimflam::kL2tpIpsecPskProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 261 | if (!psk.empty()) { |
| 262 | if (!file_util::CreateTemporaryFileInDir( |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 263 | manager()->run_path(), &psk_file_) || |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 264 | chmod(psk_file_.value().c_str(), S_IRUSR | S_IWUSR) || |
| 265 | file_util::WriteFile(psk_file_, psk.data(), psk.size()) != |
| 266 | static_cast<int>(psk.size())) { |
| 267 | Error::PopulateAndLog( |
| 268 | error, Error::kInternalError, "Unable to setup psk file."); |
| 269 | return false; |
| 270 | } |
| 271 | options->push_back("--psk_file"); |
| 272 | options->push_back(psk_file_.value()); |
| 273 | } |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | void L2TPIPSecDriver::InitNSSOptions(vector<string> *options) { |
| 278 | string ca_cert = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 279 | args()->LookupString(flimflam::kL2tpIpsecCaCertNssProperty, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 280 | if (!ca_cert.empty()) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 281 | const string &vpnhost = args()->GetString(flimflam::kProviderHostProperty); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 282 | vector<char> id(vpnhost.begin(), vpnhost.end()); |
| 283 | FilePath certfile = nss_->GetDERCertfile(ca_cert, id); |
| 284 | if (certfile.empty()) { |
| 285 | LOG(ERROR) << "Unable to extract certificate: " << ca_cert; |
| 286 | } else { |
| 287 | options->push_back("--server_ca_file"); |
| 288 | options->push_back(certfile.value()); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | bool L2TPIPSecDriver::AppendValueOption( |
| 294 | const string &property, const string &option, vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 295 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 296 | if (!value.empty()) { |
| 297 | options->push_back(option); |
| 298 | options->push_back(value); |
| 299 | return true; |
| 300 | } |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | bool L2TPIPSecDriver::AppendFlag(const string &property, |
| 305 | const string &true_option, |
| 306 | const string &false_option, |
| 307 | vector<string> *options) { |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 308 | string value = args()->LookupString(property, ""); |
Darin Petkov | f7ef50a | 2012-04-16 20:54:31 +0200 | [diff] [blame] | 309 | if (!value.empty()) { |
| 310 | options->push_back(value == "true" ? true_option : false_option); |
| 311 | return true; |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 316 | // static |
| 317 | void L2TPIPSecDriver::OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data) { |
| 318 | SLOG(VPN, 2) << __func__ << "(" << pid << ", " << status << ")"; |
| 319 | L2TPIPSecDriver *me = reinterpret_cast<L2TPIPSecDriver *>(data); |
| 320 | me->child_watch_tag_ = 0; |
| 321 | CHECK_EQ(pid, me->pid_); |
Darin Petkov | 5a85047 | 2012-06-06 15:44:24 +0200 | [diff] [blame] | 322 | me->pid_ = 0; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 323 | me->Cleanup(Service::kStateFailure); |
| 324 | // TODO(petkov): Figure if we need to restart the connection. |
| 325 | } |
| 326 | |
| 327 | void L2TPIPSecDriver::GetLogin(string *user, string *password) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 328 | LOG(INFO) << "Login requested."; |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 329 | string user_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 330 | args()->LookupString(flimflam::kL2tpIpsecUserProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 331 | if (user_property.empty()) { |
| 332 | LOG(ERROR) << "User not set."; |
| 333 | return; |
| 334 | } |
| 335 | string password_property = |
Darin Petkov | 01c6604 | 2012-04-26 11:10:45 +0200 | [diff] [blame] | 336 | args()->LookupString(flimflam::kL2tpIpsecPasswordProperty, ""); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 337 | if (password_property.empty()) { |
| 338 | LOG(ERROR) << "Password not set."; |
| 339 | return; |
| 340 | } |
| 341 | *user = user_property; |
| 342 | *password = password_property; |
| 343 | } |
| 344 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 345 | void L2TPIPSecDriver::ParseIPConfiguration( |
| 346 | const map<string, string> &configuration, |
| 347 | IPConfig::Properties *properties, |
| 348 | string *interface_name) { |
| 349 | properties->address_family = IPAddress::kFamilyIPv4; |
| 350 | properties->subnet_prefix = IPAddress::GetMaxPrefixLength( |
| 351 | properties->address_family); |
| 352 | for (map<string, string>::const_iterator it = configuration.begin(); |
| 353 | it != configuration.end(); ++it) { |
| 354 | const string &key = it->first; |
| 355 | const string &value = it->second; |
| 356 | SLOG(VPN, 2) << "Processing: " << key << " -> " << value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 357 | if (key == kL2TPIPSecInternalIP4Address) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 358 | properties->address = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 359 | } else if (key == kL2TPIPSecExternalIP4Address) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 360 | properties->peer_address = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 361 | } else if (key == kL2TPIPSecGatewayAddress) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 362 | properties->gateway = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 363 | } else if (key == kL2TPIPSecDNS1) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 364 | properties->dns_servers.insert(properties->dns_servers.begin(), value); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 365 | } else if (key == kL2TPIPSecDNS2) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 366 | properties->dns_servers.push_back(value); |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 367 | } else if (key == kL2TPIPSecInterfaceName) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 368 | *interface_name = value; |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 369 | } else if (key == kL2TPIPSecLNSAddress) { |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 370 | properties->trusted_ip = value; |
| 371 | } else { |
| 372 | SLOG(VPN, 2) << "Key ignored."; |
| 373 | } |
| 374 | } |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 375 | |
| 376 | // There is no IPv6 support for L2TP/IPsec VPN at this moment, so create a |
| 377 | // blackhole route for IPv6 traffic after establishing a IPv4 VPN. |
| 378 | // TODO(benchan): Generalize this when IPv6 support is added. |
| 379 | properties->blackhole_ipv6 = true; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 382 | void L2TPIPSecDriver::Notify( |
| 383 | const string &reason, const map<string, string> &dict) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 384 | LOG(INFO) << "IP configuration received: " << reason; |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 385 | |
Darin Petkov | 95f317f | 2012-10-22 13:37:43 +0200 | [diff] [blame] | 386 | if (reason != kL2TPIPSecReasonConnect) { |
Darin Petkov | a0e645e | 2012-04-25 11:38:59 +0200 | [diff] [blame] | 387 | device_->OnDisconnected(); |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 391 | DeletePSKFile(); |
| 392 | |
Darin Petkov | 0e9735d | 2012-04-24 12:33:45 +0200 | [diff] [blame] | 393 | IPConfig::Properties properties; |
| 394 | string interface_name; |
| 395 | ParseIPConfiguration(dict, &properties, &interface_name); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 396 | |
| 397 | int interface_index = device_info_->GetIndex(interface_name); |
| 398 | if (interface_index < 0) { |
| 399 | // TODO(petkov): Consider handling the race when the RTNL notification about |
| 400 | // 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] | 401 | // configuration and apply it when ClaimInterface is |
| 402 | // invoked. crosbug.com/29970. |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 403 | NOTIMPLEMENTED() << ": No device info for " << interface_name << "."; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (!device_) { |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 408 | device_ = new VPN(control_, dispatcher(), metrics_, manager(), |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 409 | interface_name, interface_index); |
| 410 | } |
| 411 | device_->SetEnabled(true); |
| 412 | device_->SelectService(service_); |
Darin Petkov | f8046b8 | 2012-04-24 16:29:23 +0200 | [diff] [blame] | 413 | device_->UpdateIPConfig(properties); |
Darin Petkov | 602303f | 2012-06-06 12:15:59 +0200 | [diff] [blame] | 414 | StopConnectTimeout(); |
Darin Petkov | 209e629 | 2012-04-20 11:33:32 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 417 | KeyValueStore L2TPIPSecDriver::GetProvider(Error *error) { |
| 418 | SLOG(VPN, 2) << __func__; |
| 419 | KeyValueStore props = VPNDriver::GetProvider(error); |
| 420 | props.SetBool(flimflam::kPassphraseRequiredProperty, |
| 421 | args()->LookupString( |
| 422 | flimflam::kL2tpIpsecPasswordProperty, "").empty()); |
| 423 | props.SetBool(flimflam::kL2tpIpsecPskRequiredProperty, |
| 424 | args()->LookupString( |
| 425 | flimflam::kL2tpIpsecPskProperty, "").empty()); |
| 426 | return props; |
| 427 | } |
| 428 | |
Darin Petkov | 7476a26 | 2012-04-12 16:30:46 +0200 | [diff] [blame] | 429 | } // namespace shill |