File: dcocli.hpp

package info (click to toggle)
openvpn3-client 24.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 19,384 kB
  • sloc: cpp: 180,128; python: 11,591; ansic: 1,878; sh: 1,767; java: 402; lisp: 81; makefile: 44
file content (357 lines) | stat: -rw-r--r-- 10,085 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//    OpenVPN -- An application to securely tunnel IP networks
//               over a single port, with support for SSL/TLS-based
//               session authentication and key exchange,
//               packet encryption, packet authentication, and
//               packet compression.
//
//    Copyright (C) 2012-2022 OpenVPN Inc.
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Affero General Public License Version 3
//    as published by the Free Software Foundation.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program in the COPYING file.
//    If not, see <http://www.gnu.org/licenses/>.

#pragma once

#include <memory>
#include <sstream>
#include <vector>

#include <openvpn/buffer/asiobuf.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/size.hpp>
#include <openvpn/common/to_string.hpp>
#include <openvpn/time/time.hpp>
#include <openvpn/transport/client/transbase.hpp>
#include <openvpn/transport/dco.hpp>
#include <openvpn/tun/tunmtu.hpp>
#include <openvpn/tun/builder/capture.hpp>
#include <openvpn/tun/client/tunbase.hpp>

#if !defined(ENABLE_OVPNDCOWIN)
#include <openvpn/tun/linux/client/tunmethods.hpp>
#endif

#if defined(ENABLE_KOVPN)
#include <openvpn/kovpn/kodevtun.hpp>
#include <openvpn/kovpn/kostats.hpp>
#include <openvpn/kovpn/kovpn.hpp>
#include <openvpn/kovpn/rps_xps.hpp>
#elif defined(ENABLE_OVPNDCO)
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/common/uniqueptr.hpp>
#include <openvpn/dco/key.hpp>
#include <openvpn/tun/linux/client/genl.hpp>
#include <openvpn/tun/linux/client/sitnl.hpp>
#elif defined(ENABLE_OVPNDCOWIN)
#include <bcrypt.h>
#include <openvpn/dco/key.hpp>
#include <openvpn/dco/ovpn-dco.h>
#include <openvpn/win/modname.hpp>
#else
#error either ENABLE_KOVPN, ENABLE_OVPNDCO or ENABLE_OVPNDCOWIN must be defined
#endif

#include <openvpn/dco/korekey.hpp>

// client-side DCO (Data Channel Offload) module for Linux/kovpn

namespace openvpn {
namespace DCOTransport {
enum
{
    OVPN_PEER_ID_UNDEF = 0x00FFFFFF,
};

class ClientConfig : public DCO,
                     public TransportClientFactory,
                     public TunClientFactory
{
  public:
    typedef RCPtr<ClientConfig> Ptr;

    std::string dev_name;

    DCO::TransportConfig transport;
    DCO::TunConfig tun;

    unsigned int ping_restart_override = 0;

    void process_push(const OptionList &opt) override
    {
        transport.remote_list->process_push(opt);
    }

    virtual void finalize(const bool disconnected) override
    {
#if defined(ENABLE_OVPNDCOWIN)
        if (disconnected)
            tun.tun_persist.reset();
#endif
    }

    virtual TunClientFactory::Ptr
    new_tun_factory(const DCO::TunConfig &conf, const OptionList &opt) override
    {
        tun = conf;

        // set a default MTU
        if (!tun.tun_prop.mtu)
            tun.tun_prop.mtu = TUN_MTU_DEFAULT;

        // parse "dev" option
        {
            const Option *dev = opt.get_ptr("dev");
            if (dev)
                dev_name = dev->get(1, 64);
            else
                dev_name = "ovpnc";
        }

        // parse ping-restart-override
        ping_restart_override = opt.get_num<decltype(ping_restart_override)>(
            "ping-restart-override", 1, ping_restart_override, 0, 3600);

        return TunClientFactory::Ptr(this);
    }

    virtual TransportClientFactory::Ptr
    new_transport_factory(const DCO::TransportConfig &conf) override
    {
        transport = conf;
        return TransportClientFactory::Ptr(this);
    }

    virtual TunClient::Ptr new_tun_client_obj(openvpn_io::io_context &io_context,
                                              TunClientParent &parent,
                                              TransportClient *transcli) override;

    virtual TransportClient::Ptr
    new_transport_client_obj(openvpn_io::io_context &io_context,
                             TransportClientParent *parent) override;

    static DCO::Ptr new_controller(TunBuilderBase *tb)
    {
        auto ctrl = new ClientConfig();
        if (ctrl)
            ctrl->builder = tb;
        return ctrl;
    }

  protected:
    ClientConfig() = default;
};

class Client : public TransportClient,
               public TunClient,
               public AsyncResolvableUDP
{
    friend class ClientConfig;

    typedef RCPtr<Client> Ptr;

  public:
    // transport methods

    virtual bool transport_send_queue_empty() override
    {
        return false;
    }

    virtual bool transport_has_send_queue() override
    {
        return false;
    }

    virtual size_t transport_send_queue_size() override
    {
        return 0;
    }

    virtual void reset_align_adjust(const size_t align_adjust) override
    {
    }

    virtual void transport_stop_requeueing() override
    {
    }

    virtual void server_endpoint_info(std::string &host,
                                      std::string &port,
                                      std::string &proto,
                                      std::string &ip_addr) const override
    {
        host = server_host;
        port = server_port;
        const IP::Addr addr = server_endpoint_addr();
        proto = std::string(transport_protocol().str());
        proto += "-DCO";
        ip_addr = addr.to_string();
    }

    virtual void stop() override
    {
        stop_();
    }

    // tun methods

    virtual void set_disconnect() override
    {
    }

    virtual bool
    tun_send(BufferAllocated &buf) override // return true if send succeeded
    {
        return false;
    }

    virtual std::string vpn_ip4() const override
    {
        if (state->vpn_ip4_addr.specified())
            return state->vpn_ip4_addr.to_string();
        else
            return "";
    }

    virtual std::string vpn_ip6() const override
    {
        if (state->vpn_ip6_addr.specified())
            return state->vpn_ip6_addr.to_string();
        else
            return "";
    }

    virtual std::string vpn_gw4() const override
    {
        if (state->vpn_ip4_gw.specified())
            return state->vpn_ip4_gw.to_string();
        else
            return "";
    }

    virtual std::string vpn_gw6() const override
    {
        if (state->vpn_ip6_gw.specified())
            return state->vpn_ip6_gw.to_string();
        else
            return "";
    }

    int vpn_mtu() const override
    {
        return state->mtu;
    }

  protected:
    Client(openvpn_io::io_context &io_context_arg,
           ClientConfig *config_arg,
           TransportClientParent *parent_arg)
        : AsyncResolvableUDP(io_context_arg), io_context(io_context_arg),
          halt(false), state(new TunProp::State()), config(config_arg),
          transport_parent(parent_arg), tun_parent(nullptr),
          peer_id(OVPN_PEER_ID_UNDEF)
    {
    }

    virtual void transport_reparent(TransportClientParent *parent_arg) override
    {
        transport_parent = parent_arg;
    }

    virtual void stop_() = 0;

    openvpn_io::io_context &io_context;
    bool halt;

    TunProp::State::Ptr state;

    ClientConfig::Ptr config;
    TransportClientParent *transport_parent;
    TunClientParent *tun_parent;

    ActionList::Ptr remove_cmds;

    std::string server_host;
    std::string server_port;

    uint32_t peer_id;
};

#if defined(ENABLE_KOVPN)
#include <openvpn/kovpn/kovpncli.hpp>
inline DCO::Ptr new_controller(TunBuilderBase *)
{
    return KovpnClientConfig::new_controller();
}
inline TransportClient::Ptr
ClientConfig::new_transport_client_obj(openvpn_io::io_context &io_context,
                                       TransportClientParent *parent)
{
    return TransportClient::Ptr(new KovpnClient(io_context, this, parent));
}
#elif defined(ENABLE_OVPNDCO)
#include <openvpn/dco/ovpndcocli.hpp>
inline DCO::Ptr new_controller(TunBuilderBase *tb)
{
    if (!OvpnDcoClient::available(tb))
        return nullptr;

    CryptoAlgs::allow_dc_algs({CryptoAlgs::CHACHA20_POLY1305,
                               CryptoAlgs::AES_128_GCM,
                               CryptoAlgs::AES_192_GCM,
                               CryptoAlgs::AES_256_GCM});
    return ClientConfig::new_controller(tb);
}
inline TransportClient::Ptr
ClientConfig::new_transport_client_obj(openvpn_io::io_context &io_context,
                                       TransportClientParent *parent)
{
    return TransportClient::Ptr(new OvpnDcoClient(io_context, this, parent));
}
#elif defined(ENABLE_OVPNDCOWIN)
#include <openvpn/dco/ovpndcowincli.hpp>
inline DCO::Ptr new_controller(TunBuilderBase *tb)
{
    if (!OvpnDcoWinClient::available())
        return nullptr;

    std::list<CryptoAlgs::Type> algs{CryptoAlgs::AES_128_GCM, CryptoAlgs::AES_192_GCM, CryptoAlgs::AES_256_GCM};
    BCRYPT_ALG_HANDLE h;
    NTSTATUS status = BCryptOpenAlgorithmProvider(&h, L"CHACHA20_POLY1305", NULL, 0);
    if (BCRYPT_SUCCESS(status))
    {
        BCryptCloseAlgorithmProvider(h, 0);
        algs.push_back(CryptoAlgs::CHACHA20_POLY1305);
    }

    CryptoAlgs::allow_dc_algs(algs);
    return ClientConfig::new_controller(nullptr);
}
inline TransportClient::Ptr
ClientConfig::new_transport_client_obj(openvpn_io::io_context &io_context,
                                       TransportClientParent *parent)
{
    return TransportClient::Ptr(new OvpnDcoWinClient(io_context, this, parent));
}
#endif

inline TunClient::Ptr
ClientConfig::new_tun_client_obj(openvpn_io::io_context &io_context,
                                 TunClientParent &parent,
                                 TransportClient *transcli)
{
    Client *cli = static_cast<Client *>(transcli);
    cli->tun_parent = &parent;
    return TunClient::Ptr(cli);
}
} // namespace DCOTransport
} // namespace openvpn