blob: 4ff6489ef3dcba70add50490e6ea29811d6ecbfd [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 Petkov95f317f2012-10-22 13:37:43 +020022// Declared in the header to avoid linking unused code into shims.
23static const char kL2TPIPSecDNS1[] = "DNS1";
24static const char kL2TPIPSecDNS2[] = "DNS2";
25static const char kL2TPIPSecExternalIP4Address[] = "EXTERNAL_IP4_ADDRESS";
26static const char kL2TPIPSecGatewayAddress[] = "GATEWAY_ADDRESS";
27static const char kL2TPIPSecInterfaceName[] = "INTERNAL_IFNAME";
28static const char kL2TPIPSecInternalIP4Address[] = "INTERNAL_IP4_ADDRESS";
29static const char kL2TPIPSecLNSAddress[] = "LNS_ADDRESS";
30static const char kL2TPIPSecReasonConnect[] = "connect";
31static const char kL2TPIPSecReasonDisconnect[] = "disconnect";
32
Darin Petkov209e6292012-04-20 11:33:32 +020033class ControlInterface;
Darin Petkovf8046b82012-04-24 16:29:23 +020034class DeviceInfo;
Darin Petkov209e6292012-04-20 11:33:32 +020035class GLib;
Darin Petkovf8046b82012-04-24 16:29:23 +020036class Metrics;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020037class NSS;
Darin Petkov5a850472012-06-06 15:44:24 +020038class ProcessKiller;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020039
Darin Petkov209e6292012-04-20 11:33:32 +020040class L2TPIPSecDriver : public VPNDriver,
41 public RPCTaskDelegate {
Darin Petkov7476a262012-04-12 16:30:46 +020042 public:
Darin Petkovf8046b82012-04-24 16:29:23 +020043 L2TPIPSecDriver(ControlInterface *control,
44 EventDispatcher *dispatcher,
45 Metrics *metrics,
46 Manager *manager,
47 DeviceInfo *device_info,
48 GLib *glib);
Darin Petkov7476a262012-04-12 16:30:46 +020049 virtual ~L2TPIPSecDriver();
50
51 // Inherited from VPNDriver.
52 virtual bool ClaimInterface(const std::string &link_name,
53 int interface_index);
54 virtual void Connect(const VPNServiceRefPtr &service, Error *error);
55 virtual void Disconnect();
Darin Petkov5eb05422012-05-11 15:45:25 +020056 virtual void OnConnectionDisconnected();
Darin Petkov7476a262012-04-12 16:30:46 +020057 virtual std::string GetProviderType() const;
58
59 private:
60 friend class L2TPIPSecDriverTest;
Darin Petkovf7ef50a2012-04-16 20:54:31 +020061 FRIEND_TEST(L2TPIPSecDriverTest, AppendFlag);
62 FRIEND_TEST(L2TPIPSecDriverTest, AppendValueOption);
63 FRIEND_TEST(L2TPIPSecDriverTest, Cleanup);
Darin Petkov602303f2012-06-06 12:15:59 +020064 FRIEND_TEST(L2TPIPSecDriverTest, Connect);
Darin Petkov0e9735d2012-04-24 12:33:45 +020065 FRIEND_TEST(L2TPIPSecDriverTest, DeletePSKFile);
Darin Petkova0e645e2012-04-25 11:38:59 +020066 FRIEND_TEST(L2TPIPSecDriverTest, Disconnect);
Darin Petkov209e6292012-04-20 11:33:32 +020067 FRIEND_TEST(L2TPIPSecDriverTest, GetLogin);
68 FRIEND_TEST(L2TPIPSecDriverTest, InitEnvironment);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020069 FRIEND_TEST(L2TPIPSecDriverTest, InitNSSOptions);
70 FRIEND_TEST(L2TPIPSecDriverTest, InitOptions);
71 FRIEND_TEST(L2TPIPSecDriverTest, InitOptionsNoHost);
72 FRIEND_TEST(L2TPIPSecDriverTest, InitPSKOptions);
Darin Petkov0e9735d2012-04-24 12:33:45 +020073 FRIEND_TEST(L2TPIPSecDriverTest, Notify);
Darin Petkova0e645e2012-04-25 11:38:59 +020074 FRIEND_TEST(L2TPIPSecDriverTest, NotifyFail);
Darin Petkov5eb05422012-05-11 15:45:25 +020075 FRIEND_TEST(L2TPIPSecDriverTest, OnConnectionDisconnected);
Darin Petkov209e6292012-04-20 11:33:32 +020076 FRIEND_TEST(L2TPIPSecDriverTest, OnL2TPIPSecVPNDied);
Darin Petkov0e9735d2012-04-24 12:33:45 +020077 FRIEND_TEST(L2TPIPSecDriverTest, ParseIPConfiguration);
Darin Petkov209e6292012-04-20 11:33:32 +020078 FRIEND_TEST(L2TPIPSecDriverTest, SpawnL2TPIPSecVPN);
Darin Petkova0e645e2012-04-25 11:38:59 +020079 FRIEND_TEST(L2TPIPSecDriverTest, VerifyPaths);
Darin Petkovd4325392012-04-23 15:48:22 +020080
Darin Petkovf7ef50a2012-04-16 20:54:31 +020081 static const char kPPPDPlugin[];
Darin Petkov209e6292012-04-20 11:33:32 +020082 static const char kL2TPIPSecVPNPath[];
Darin Petkovd4325392012-04-23 15:48:22 +020083 static const Property kProperties[];
Darin Petkovf7ef50a2012-04-16 20:54:31 +020084
Darin Petkov209e6292012-04-20 11:33:32 +020085 bool SpawnL2TPIPSecVPN(Error *error);
86
87 void InitEnvironment(std::vector<std::string> *environment);
88
89 bool InitOptions(std::vector<std::string> *options, Error *error);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020090 bool InitPSKOptions(std::vector<std::string> *options, Error *error);
91 void InitNSSOptions(std::vector<std::string> *options);
92
Darin Petkov209e6292012-04-20 11:33:32 +020093 void Cleanup(Service::ConnectState state);
Darin Petkovf7ef50a2012-04-16 20:54:31 +020094
Darin Petkov0e9735d2012-04-24 12:33:45 +020095 void DeletePSKFile();
96
Darin Petkovf7ef50a2012-04-16 20:54:31 +020097 // Returns true if an opton was appended.
98 bool AppendValueOption(const std::string &property,
99 const std::string &option,
100 std::vector<std::string> *options);
101
102 // Returns true if a flag was appended.
103 bool AppendFlag(const std::string &property,
104 const std::string &true_option,
105 const std::string &false_option,
106 std::vector<std::string> *options);
107
Darin Petkov0e9735d2012-04-24 12:33:45 +0200108 static void ParseIPConfiguration(
109 const std::map<std::string, std::string> &configuration,
110 IPConfig::Properties *properties,
111 std::string *interface_name);
112
Darin Petkov209e6292012-04-20 11:33:32 +0200113 // Called when the l2tpipsec_vpn process exits.
114 static void OnL2TPIPSecVPNDied(GPid pid, gint status, gpointer data);
115
Darin Petkovb536a742012-04-26 11:31:28 +0200116 // Inherit from VPNDriver to add custom properties.
117 virtual KeyValueStore GetProvider(Error *error);
118
Darin Petkov209e6292012-04-20 11:33:32 +0200119 // Implements RPCTaskDelegate.
120 virtual void GetLogin(std::string *user, std::string *password);
121 virtual void Notify(const std::string &reason,
122 const std::map<std::string, std::string> &dict);
123
124 ControlInterface *control_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200125 Metrics *metrics_;
126 DeviceInfo *device_info_;
Darin Petkov209e6292012-04-20 11:33:32 +0200127 GLib *glib_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200128 NSS *nss_;
Darin Petkov5a850472012-06-06 15:44:24 +0200129 ProcessKiller *process_killer_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200130
Darin Petkov209e6292012-04-20 11:33:32 +0200131 VPNServiceRefPtr service_;
132 scoped_ptr<RPCTask> rpc_task_;
Darin Petkovf7ef50a2012-04-16 20:54:31 +0200133 FilePath psk_file_;
Darin Petkovf8046b82012-04-24 16:29:23 +0200134 VPNRefPtr device_;
Darin Petkov7476a262012-04-12 16:30:46 +0200135
Darin Petkov209e6292012-04-20 11:33:32 +0200136 // The PID of the spawned l2tpipsec_vpn process. May be 0 if no process has
137 // been spawned yet or the process has died.
138 int pid_;
139
140 // Child exit watch callback source tag.
141 unsigned int child_watch_tag_;
142
Darin Petkov7476a262012-04-12 16:30:46 +0200143 DISALLOW_COPY_AND_ASSIGN(L2TPIPSecDriver);
144};
145
146} // namespace shill
147
148#endif // SHILL_L2TP_IPSEC_DRIVER_