blob: a6833adae469f172a80af2994ba92ad4b5fbf70f [file] [log] [blame]
Darin Petkov7476a262012-04-12 16:30:46 +02001// 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
Darin Petkova42afe32013-02-05 16:53:52 +01005#ifndef SHILL_L2TP_IPSEC_DRIVER_H_
6#define SHILL_L2TP_IPSEC_DRIVER_H_
Darin Petkov7476a262012-04-12 16:30:46 +02007
mukesh agrawalae30e9e2013-05-28 14:09:16 -07008#include <sys/types.h>
9
Darin Petkovf7ef50a2012-04-16 20:54:31 +020010#include <vector>
11
12#include <base/file_path.h>
Darin Petkov209e6292012-04-20 11:33:32 +020013#include <base/memory/scoped_ptr.h>
Darin Petkovf7ef50a2012-04-16 20:54:31 +020014#include <gtest/gtest_prod.h> // for FRIEND_TEST
15
Darin Petkov0e9735d2012-04-24 12:33:45 +020016#include "shill/ipconfig.h"
Darin Petkov209e6292012-04-20 11:33:32 +020017#include "shill/rpc_task.h"
18#include "shill/service.h"
Darin Petkov7476a262012-04-12 16:30:46 +020019#include "shill/vpn_driver.h"
20
21namespace shill {
22
Paul Stewart5baebb72013-03-14 11:43:29 -070023class CertificateFile;
Darin Petkov209e6292012-04-20 11:33:32 +020024class ControlInterface;
Darin Petkovf8046b82012-04-24 16:29:23 +020025class DeviceInfo;
mukesh agrawalae30e9e2013-05-28 14:09:16 -070026class ExternalTask;
Darin Petkov209e6292012-04-20 11:33:32 +020027class GLib;
Darin Petkovf8046b82012-04-24 16:29:23 +020028class Metrics;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020029class NSS;
30
Darin Petkov209e6292012-04-20 11:33:32 +020031class L2TPIPSecDriver : public VPNDriver,
32 public RPCTaskDelegate {
Darin Petkov7476a262012-04-12 16:30:46 +020033 public:
Darin Petkovf8046b82012-04-24 16:29:23 +020034 L2TPIPSecDriver(ControlInterface *control,
35 EventDispatcher *dispatcher,
36 Metrics *metrics,
37 Manager *manager,
38 DeviceInfo *device_info,
39 GLib *glib);
Darin Petkov7476a262012-04-12 16:30:46 +020040 virtual ~L2TPIPSecDriver();
41
Darin Petkova42afe32013-02-05 16:53:52 +010042 protected:
Darin Petkov7476a262012-04-12 16:30:46 +020043 // Inherited from VPNDriver.
44 virtual bool ClaimInterface(const std::string &link_name,
45 int interface_index);
46 virtual void Connect(const VPNServiceRefPtr &service, Error *error);
47 virtual void Disconnect();
Darin Petkov7476a262012-04-12 16:30:46 +020048 virtual std::string GetProviderType() const;
Darin Petkova42afe32013-02-05 16:53:52 +010049 virtual void OnConnectionDisconnected();
50 virtual void OnConnectTimeout();
Darin Petkov7476a262012-04-12 16:30:46 +020051
52 private:
53 friend class L2TPIPSecDriverTest;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020054 FRIEND_TEST(L2TPIPSecDriverTest, AppendFlag);
55 FRIEND_TEST(L2TPIPSecDriverTest, AppendValueOption);
56 FRIEND_TEST(L2TPIPSecDriverTest, Cleanup);
Darin Petkov602303f2012-06-06 12:15:59 +020057 FRIEND_TEST(L2TPIPSecDriverTest, Connect);
Darin Petkov0e9735d2012-04-24 12:33:45 +020058 FRIEND_TEST(L2TPIPSecDriverTest, DeletePSKFile);
Darin Petkova0e645e2012-04-25 11:38:59 +020059 FRIEND_TEST(L2TPIPSecDriverTest, Disconnect);
Darin Petkov209e6292012-04-20 11:33:32 +020060 FRIEND_TEST(L2TPIPSecDriverTest, GetLogin);
61 FRIEND_TEST(L2TPIPSecDriverTest, InitEnvironment);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020062 FRIEND_TEST(L2TPIPSecDriverTest, InitNSSOptions);
Paul Stewart5baebb72013-03-14 11:43:29 -070063 FRIEND_TEST(L2TPIPSecDriverTest, InitPEMOptions);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020064 FRIEND_TEST(L2TPIPSecDriverTest, InitOptions);
65 FRIEND_TEST(L2TPIPSecDriverTest, InitOptionsNoHost);
66 FRIEND_TEST(L2TPIPSecDriverTest, InitPSKOptions);
Darin Petkov69990222012-11-14 09:25:25 +010067 FRIEND_TEST(L2TPIPSecDriverTest, NotifyDisconnected);
Darin Petkov5eb05422012-05-11 15:45:25 +020068 FRIEND_TEST(L2TPIPSecDriverTest, OnConnectionDisconnected);
Darin Petkov209e6292012-04-20 11:33:32 +020069 FRIEND_TEST(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied);
Darin Petkov0e9735d2012-04-24 12:33:45 +020070 FRIEND_TEST(L2TPIPSecDriverTest, ParseIPConfiguration);
Darin Petkov209e6292012-04-20 11:33:32 +020071 FRIEND_TEST(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN);
Darin Petkova0e645e2012-04-25 11:38:59 +020072 FRIEND_TEST(L2TPIPSecDriverTest, VerifyPaths);
Darin Petkovd4325392012-04-23 15:48:22 +020073
Darin Petkov209e6292012-04-20 11:33:32 +020074 static const char kL2TPIPSecVPNPath[];
Darin Petkovd4325392012-04-23 15:48:22 +020075 static const Property kProperties[];
Darin Petkovf7ef50a2012-04-16 20:54:31 +020076
Darin Petkov209e6292012-04-20 11:33:32 +020077 bool SpawnL2TPIPSecVPN(Error *error);
78
Darin Petkov209e6292012-04-20 11:33:32 +020079 bool InitOptions(std::vector<std::string> *options, Error *error);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020080 bool InitPSKOptions(std::vector<std::string> *options, Error *error);
81 void InitNSSOptions(std::vector<std::string> *options);
Paul Stewart5baebb72013-03-14 11:43:29 -070082 bool InitPEMOptions(std::vector<std::string> *options);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020083
Darin Petkov85d53172013-03-13 16:43:28 +010084 // Resets the VPN state and deallocates all resources. If there's a service
85 // associated through Connect, sets its state to Service::kStateIdle and
86 // disassociates from the service.
87 void IdleService();
88
89 // Resets the VPN state and deallocates all resources. If there's a service
90 // associated through Connect, sets its state to Service::kStateFailure with
91 // failure reason |failure| and disassociates from the service.
92 void FailService(Service::ConnectFailure failure);
93
94 // Implements the IdleService and FailService methods. Resets the VPN state
95 // and deallocates all resources. If there's a service associated through
96 // Connect, sets its state |state|; if |state| is Service::kStateFailure, sets
97 // the failure reason to |failure|; disassociates from the service.
98 void Cleanup(Service::ConnectState state, Service::ConnectFailure failure);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020099
Darin Petkov0e9735d2012-04-24 12:33:45 +0200100 void DeletePSKFile();
101
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200102 // Returns true if an opton was appended.
103 bool AppendValueOption(const std::string &property,
104 const std::string &option,
105 std::vector<std::string> *options);
106
107 // Returns true if a flag was appended.
108 bool AppendFlag(const std::string &property,
109 const std::string &true_option,
110 const std::string &false_option,
111 std::vector<std::string> *options);
112
Darin Petkov85d53172013-03-13 16:43:28 +0100113 static Service::ConnectFailure TranslateExitStatusToFailure(int status);
114
Darin Petkovb536a742012-04-26 11:31:28 +0200115 // Inherit from VPNDriver to add custom properties.
116 virtual KeyValueStore GetProvider(Error *error);
117
Darin Petkov209e6292012-04-20 11:33:32 +0200118 // Implements RPCTaskDelegate.
119 virtual void GetLogin(std::string *user, std::string *password);
120 virtual void Notify(const std::string &reason,
121 const std::map<std::string, std::string> &dict);
mukesh agrawalae30e9e2013-05-28 14:09:16 -0700122 // Called when the l2tpipsec_vpn process exits.
123 void OnL2TPIPSecVPNDied(pid_t pid, int status);
Darin Petkov209e6292012-04-20 11:33:32 +0200124
Paul Stewart91a43cb2013-03-02 21:34:15 -0800125 void ReportConnectionMetrics();
126
Darin Petkov209e6292012-04-20 11:33:32 +0200127 ControlInterface *control_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200128 Metrics *metrics_;
129 DeviceInfo *device_info_;
Darin Petkov209e6292012-04-20 11:33:32 +0200130 GLib *glib_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200131 NSS *nss_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200132
Darin Petkov209e6292012-04-20 11:33:32 +0200133 VPNServiceRefPtr service_;
mukesh agrawalae30e9e2013-05-28 14:09:16 -0700134 scoped_ptr<ExternalTask> external_task_;
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800135 base::FilePath psk_file_;
Paul Stewart5baebb72013-03-14 11:43:29 -0700136 scoped_ptr<CertificateFile> certificate_file_;
mukesh agrawal9da07772013-05-15 14:15:17 -0700137 PPPDeviceRefPtr device_;
mukesh agrawalae30e9e2013-05-28 14:09:16 -0700138 base::WeakPtrFactory<L2TPIPSecDriver> weak_ptr_factory_;
Darin Petkov209e6292012-04-20 11:33:32 +0200139
Darin Petkov7476a262012-04-12 16:30:46 +0200140 DISALLOW_COPY_AND_ASSIGN(L2TPIPSecDriver);
141};
142
143} // namespace shill
144
Darin Petkova42afe32013-02-05 16:53:52 +0100145#endif // SHILL_L2TP_IPSEC_DRIVER_H_