blob: d624c836cd7fb10cb400e941f7d4e3e81fe543b4 [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
5#ifndef SHILL_L2TP_IPSEC_DRIVER_
6#define SHILL_L2TP_IPSEC_DRIVER_
7
Darin Petkovf7ef50a2012-04-16 20:54:31 +02008#include <vector>
9
10#include <base/file_path.h>
Darin Petkov209e6292012-04-20 11:33:32 +020011#include <base/memory/scoped_ptr.h>
Darin Petkovf7ef50a2012-04-16 20:54:31 +020012#include <gtest/gtest_prod.h> // for FRIEND_TEST
13
Darin Petkov209e6292012-04-20 11:33:32 +020014#include "shill/glib.h"
Darin Petkov0e9735d2012-04-24 12:33:45 +020015#include "shill/ipconfig.h"
Darin Petkov209e6292012-04-20 11:33:32 +020016#include "shill/rpc_task.h"
17#include "shill/service.h"
Darin Petkov7476a262012-04-12 16:30:46 +020018#include "shill/vpn_driver.h"
19
20namespace shill {
21
Darin Petkov209e6292012-04-20 11:33:32 +020022class ControlInterface;
Darin Petkovf8046b82012-04-24 16:29:23 +020023class DeviceInfo;
24class EventDispatcher;
Darin Petkov209e6292012-04-20 11:33:32 +020025class GLib;
Darin Petkovf8046b82012-04-24 16:29:23 +020026class Metrics;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020027class NSS;
28
Darin Petkov209e6292012-04-20 11:33:32 +020029class L2TPIPSecDriver : public VPNDriver,
30 public RPCTaskDelegate {
Darin Petkov7476a262012-04-12 16:30:46 +020031 public:
Darin Petkovf8046b82012-04-24 16:29:23 +020032 L2TPIPSecDriver(ControlInterface *control,
33 EventDispatcher *dispatcher,
34 Metrics *metrics,
35 Manager *manager,
36 DeviceInfo *device_info,
37 GLib *glib);
Darin Petkov7476a262012-04-12 16:30:46 +020038 virtual ~L2TPIPSecDriver();
39
40 // Inherited from VPNDriver.
41 virtual bool ClaimInterface(const std::string &link_name,
42 int interface_index);
43 virtual void Connect(const VPNServiceRefPtr &service, Error *error);
44 virtual void Disconnect();
Darin Petkov5eb05422012-05-11 15:45:25 +020045 virtual void OnConnectionDisconnected();
Darin Petkov7476a262012-04-12 16:30:46 +020046 virtual std::string GetProviderType() const;
47
48 private:
49 friend class L2TPIPSecDriverTest;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020050 FRIEND_TEST(L2TPIPSecDriverTest, AppendFlag);
51 FRIEND_TEST(L2TPIPSecDriverTest, AppendValueOption);
52 FRIEND_TEST(L2TPIPSecDriverTest, Cleanup);
Darin Petkov0e9735d2012-04-24 12:33:45 +020053 FRIEND_TEST(L2TPIPSecDriverTest, DeletePSKFile);
Darin Petkova0e645e2012-04-25 11:38:59 +020054 FRIEND_TEST(L2TPIPSecDriverTest, Disconnect);
Darin Petkov209e6292012-04-20 11:33:32 +020055 FRIEND_TEST(L2TPIPSecDriverTest, GetLogin);
56 FRIEND_TEST(L2TPIPSecDriverTest, InitEnvironment);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020057 FRIEND_TEST(L2TPIPSecDriverTest, InitNSSOptions);
58 FRIEND_TEST(L2TPIPSecDriverTest, InitOptions);
59 FRIEND_TEST(L2TPIPSecDriverTest, InitOptionsNoHost);
60 FRIEND_TEST(L2TPIPSecDriverTest, InitPSKOptions);
Darin Petkov0e9735d2012-04-24 12:33:45 +020061 FRIEND_TEST(L2TPIPSecDriverTest, Notify);
Darin Petkova0e645e2012-04-25 11:38:59 +020062 FRIEND_TEST(L2TPIPSecDriverTest, NotifyFail);
Darin Petkov5eb05422012-05-11 15:45:25 +020063 FRIEND_TEST(L2TPIPSecDriverTest, OnConnectionDisconnected);
Darin Petkov209e6292012-04-20 11:33:32 +020064 FRIEND_TEST(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied);
Darin Petkov0e9735d2012-04-24 12:33:45 +020065 FRIEND_TEST(L2TPIPSecDriverTest, ParseIPConfiguration);
Darin Petkov209e6292012-04-20 11:33:32 +020066 FRIEND_TEST(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN);
Darin Petkova0e645e2012-04-25 11:38:59 +020067 FRIEND_TEST(L2TPIPSecDriverTest, VerifyPaths);
Darin Petkovd4325392012-04-23 15:48:22 +020068
Darin Petkovf7ef50a2012-04-16 20:54:31 +020069 static const char kPPPDPlugin[];
Darin Petkov209e6292012-04-20 11:33:32 +020070 static const char kL2TPIPSecVPNPath[];
Darin Petkovd4325392012-04-23 15:48:22 +020071 static const Property kProperties[];
Darin Petkovf7ef50a2012-04-16 20:54:31 +020072
Darin Petkov209e6292012-04-20 11:33:32 +020073 bool SpawnL2TPIPSecVPN(Error *error);
74
75 void InitEnvironment(std::vector<std::string> *environment);
76
77 bool InitOptions(std::vector<std::string> *options, Error *error);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020078 bool InitPSKOptions(std::vector<std::string> *options, Error *error);
79 void InitNSSOptions(std::vector<std::string> *options);
80
Darin Petkov209e6292012-04-20 11:33:32 +020081 void Cleanup(Service::ConnectState state);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020082
Darin Petkov0e9735d2012-04-24 12:33:45 +020083 void DeletePSKFile();
84
Darin Petkovf7ef50a2012-04-16 20:54:31 +020085 // Returns true if an opton was appended.
86 bool AppendValueOption(const std::string &property,
87 const std::string &option,
88 std::vector<std::string> *options);
89
90 // Returns true if a flag was appended.
91 bool AppendFlag(const std::string &property,
92 const std::string &true_option,
93 const std::string &false_option,
94 std::vector<std::string> *options);
95
Darin Petkov0e9735d2012-04-24 12:33:45 +020096 static void ParseIPConfiguration(
97 const std::map<std::string, std::string> &configuration,
98 IPConfig::Properties *properties,
99 std::string *interface_name);
100
Darin Petkov209e6292012-04-20 11:33:32 +0200101 // Called when the l2tpipsec_vpn process exits.
102 static void OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data);
103
Darin Petkovb536a742012-04-26 11:31:28 +0200104 // Inherit from VPNDriver to add custom properties.
105 virtual KeyValueStore GetProvider(Error *error);
106
Darin Petkov209e6292012-04-20 11:33:32 +0200107 // Implements RPCTaskDelegate.
108 virtual void GetLogin(std::string *user, std::string *password);
109 virtual void Notify(const std::string &reason,
110 const std::map<std::string, std::string> &dict);
111
112 ControlInterface *control_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200113 EventDispatcher *dispatcher_;
114 Metrics *metrics_;
115 DeviceInfo *device_info_;
Darin Petkov209e6292012-04-20 11:33:32 +0200116 GLib *glib_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200117 NSS *nss_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200118
Darin Petkov209e6292012-04-20 11:33:32 +0200119 VPNServiceRefPtr service_;
120 scoped_ptr<RPCTask> rpc_task_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200121 FilePath psk_file_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200122 VPNRefPtr device_;
Darin Petkov7476a262012-04-12 16:30:46 +0200123
Darin Petkov209e6292012-04-20 11:33:32 +0200124 // The PID of the spawned l2tpipsec_vpn process. May be 0 if no process has
125 // been spawned yet or the process has died.
126 int pid_;
127
128 // Child exit watch callback source tag.
129 unsigned int child_watch_tag_;
130
Darin Petkov7476a262012-04-12 16:30:46 +0200131 DISALLOW_COPY_AND_ASSIGN(L2TPIPSecDriver);
132};
133
134} // namespace shill
135
136#endif // SHILL_L2TP_IPSEC_DRIVER_