File: cliemuexr.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (168 lines) | stat: -rw-r--r-- 6,023 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
//    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- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//

// Emulate Excluded Routes implementation (needed by Android)

#ifndef OPENVPN_CLIENT_CLIEMUEXR_H
#define OPENVPN_CLIENT_CLIEMUEXR_H

#include <openvpn/common/exception.hpp>
#include <openvpn/tun/client/emuexr.hpp>
#include <openvpn/addr/addrspacesplit.hpp>

namespace openvpn {
class EmulateExcludeRouteImpl : public EmulateExcludeRoute
{
  public:
    OPENVPN_EXCEPTION(emulate_exclude_route_error);

    typedef RCPtr<EmulateExcludeRouteImpl> Ptr;

    explicit EmulateExcludeRouteImpl(const bool exclude_server_address)
        : exclude_server_address_(exclude_server_address)
    {
    }

  private:
    void add_route(const bool add, const IP::Addr &addr, const int prefix_len) override
    {
        (add ? include : exclude).emplace_back(addr, prefix_len);
    }

    void add_default_routes(bool ipv4, bool ipv6) override
    {
        if (ipv4)
            add_route(true, IP::Addr::from_zero(IP::Addr::V4), 0);
        if (ipv6)
            add_route(true, IP::Addr::from_zero(IP::Addr::V6), 0);
    }

    bool enabled(const IPVerFlags &ipv) const override
    {
        return exclude.size() && (ipv.rgv4() || ipv.rgv6());
    }

    void emulate(TunBuilderBase *tb, IPVerFlags &ipv, const IP::Addr &server_addr) const override
    {
        const unsigned int ip_ver_flags = ipv.ip_ver_flags();
        IP::RouteList rl, tempExcludeList;
        rl.reserve(include.size() + exclude.size());
        rl.insert(rl.end(), include.begin(), include.end());
        rl.insert(rl.end(), exclude.begin(), exclude.end());

        // Check if we have to exclude the server, if yes we temporarily add it to the list
        // of excluded networks as small individual /32 or /128 network
        const IP::RouteList *excludedRoutes = &exclude;

        if (exclude_server_address_
            && (server_addr.version_mask() & ip_ver_flags)
            && !exclude.contains(IP::Route(server_addr, server_addr.size())))
        {
            rl.emplace_back(server_addr, server_addr.size());
            // Create a temporary list that includes all the routes + the server
            tempExcludeList = exclude;
            tempExcludeList.emplace_back(server_addr, server_addr.size());
            excludedRoutes = &tempExcludeList;
        }


        if (excludedRoutes->empty())
        {
            // Samsung's Android VPN API does different things if you have
            // 0.0.0.0/0 in the list of installed routes
            // (even if 0.0.0.0/1 and 128.0.0.0/1 and are present it behaves different)

            // We normally always split the address space, breaking a 0.0.0.0/0 into
            // smaller routes. If no routes are excluded, we install the original
            // routes without modifying them

            for (const auto &rt : include)
            {
                if (rt.version() & ip_ver_flags)
                {
                    if (!tb->tun_builder_add_route(rt.addr.to_string(), rt.prefix_len, -1, rt.addr.version() == IP::Addr::V6))
                        throw emulate_exclude_route_error("tun_builder_add_route failed");
                }
            }
            return;
        }

        // Complete address space (0.0.0.0/0 or ::/0) split into smaller networks
        // Figure out which parts of this non overlapping address we want to install
        for (const auto &r : IP::AddressSpaceSplitter(rl, ip_ver_flags))
        {
            if (check_route_should_be_installed(r, *excludedRoutes))
                if (!tb->tun_builder_add_route(r.addr.to_string(), r.prefix_len, -1, r.addr.version() == IP::Addr::V6))
                    throw emulate_exclude_route_error("tun_builder_add_route failed");
        }

        ipv.set_emulate_exclude_routes();
    }

    bool check_route_should_be_installed(const IP::Route &r, const IP::RouteList &excludedRoutes) const
    {
        // The whole address space was partioned into NON-overlapping routes that
        // we get one by one with the parameter r.
        // Therefore we already know that the whole route r either is included or
        // excluded IPs.
        // Figure out if this particular route should be installed or not

        IP::Route const *bestroute = nullptr;
        // Get the best (longest-prefix/smallest) route from included routes that completely
        // matches this route
        for (const auto &incRoute : include)
        {
            if (incRoute.contains(r))
            {
                if (!bestroute || bestroute->prefix_len < incRoute.prefix_len)
                    bestroute = &incRoute;
            }
        }

        // No positive route matches the route at all, do not install it
        if (!bestroute)
            return false;

        // Check if there is a more specific exclude route
        for (const auto &exclRoute : excludedRoutes)
        {
            if (exclRoute.contains(r) && exclRoute.prefix_len > bestroute->prefix_len)
                return false;
        }
        return true;
    }

    const bool exclude_server_address_;
    IP::RouteList include;
    IP::RouteList exclude;
};

class EmulateExcludeRouteFactoryImpl : public EmulateExcludeRouteFactory
{
  public:
    typedef RCPtr<EmulateExcludeRouteFactoryImpl> Ptr;

    EmulateExcludeRouteFactoryImpl(const bool exclude_server_address)
        : exclude_server_address_(exclude_server_address)
    {
    }

  private:
    EmulateExcludeRoute::Ptr new_obj() const override
    {
        return EmulateExcludeRoute::Ptr(new EmulateExcludeRouteImpl(exclude_server_address_));
    }

    const bool exclude_server_address_;
};
} // namespace openvpn

#endif