OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tunsetup.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Client tun setup for Mac
13
14#ifndef OPENVPN_TUN_MAC_CLIENT_TUNSETUP_H
15#define OPENVPN_TUN_MAC_CLIENT_TUNSETUP_H
16
17#include <string>
18#include <sstream>
19#include <ostream>
20
22#include <openvpn/common/rc.hpp>
29#include <openvpn/tun/layer.hpp>
34#include <openvpn/tun/proxy.hpp>
38
39#ifdef HAVE_JSON
41#endif
42
43namespace openvpn::TunMac {
45{
46 public:
48
49 OPENVPN_EXCEPTION(tun_mac_setup);
50
52 {
53 std::string iface_name;
54 Layer layer; // OSI layer
55 bool tun_prefix = false;
57
58#ifdef HAVE_JSON
59 Json::Value to_json() override
60 {
61 Json::Value root(Json::objectValue);
62 root["iface_name"] = Json::Value(iface_name);
63 root["layer"] = Json::Value(layer.str());
64 root["tun_prefix"] = Json::Value(tun_prefix);
65 return root;
66 };
67
68 void from_json(const Json::Value &root, const std::string &title) override
69 {
70 json::assert_dict(root, title);
71 json::to_string(root, iface_name, "iface_name", title);
72 layer = Layer::from_str(json::get_string(root, "layer", title));
73 json::to_bool(root, tun_prefix, "tun_prefix", title);
74 }
75#endif
76 };
77
78 bool add_bypass_route(const std::string &address,
79 bool ipv6,
80 std::ostream &os)
81 {
82 // not yet implemented
83 return true;
84 }
85
86 int establish(const TunBuilderCapture &pull, // defined by TunBuilderSetup::Base
88 Stop *stop,
89 std::ostream &os) override
90 {
91 // get configuration
92 Config *conf = dynamic_cast<Config *>(config);
93 if (!conf)
94 throw tun_mac_setup("missing config");
95
96 // close out old remove cmds, if they exist
97 destroy(os);
98
99 // Open tun device. Try Mac OS X integrated utun device first
100 // (layer 3 only). If utun fails and MAC_TUNTAP_FALLBACK is defined,
101 // then fall back to TunTap third-party device.
102 // If successful, conf->iface_name will be set to tun iface name.
103 int fd = -1;
104 conf->tun_prefix = false;
105 try
106 {
107#if defined(MAC_TUNTAP_FALLBACK)
108#if !defined(ASIO_DISABLE_KQUEUE)
109#error Mac OS X TunTap adapter is incompatible with kqueue; rebuild with ASIO_DISABLE_KQUEUE
110#endif
111 if (conf->layer() == Layer::OSI_LAYER_3)
112 {
113 try
114 {
115 fd = UTun::utun_open(conf->iface_name);
116 conf->tun_prefix = true;
117 }
118 catch (const std::exception &e)
119 {
120 os << e.what() << std::endl;
121 }
122 }
123 if (fd == -1)
124 fd = Util::tuntap_open(conf->layer, conf->iface_name);
125#else
126 fd = UTun::utun_open(conf->iface_name);
127 conf->tun_prefix = true;
128#endif
129 }
130 catch (const std::exception &e)
131 {
132 throw ErrorCode(Error::TUN_IFACE_CREATE, true, e.what());
133 }
134
135 // create ActionLists for setting up and removing adapter properties
136 ActionList::Ptr add_cmds(new ActionList());
138
139 // populate add/remove lists with actions
140 tun_config(conf->iface_name, pull, *add_cmds, *remove_cmds, os);
141
142 // execute the add actions and collect marks of failed actions
143 auto failed_actions = add_cmds->execute(os);
144
145 // since we should not undo failed actions, remove them
146 remove_cmds->remove_marked(failed_actions, os);
147
148 // now that the add actions have succeeded,
149 // enable the remove actions
151
152 os << "open " << conf->iface_name << " SUCCEEDED" << std::endl;
153 return fd;
154 }
155
156 void destroy(std::ostream &os) override // defined by DestructorBase
157 {
158 if (remove_cmds)
159 {
162 }
163 }
164
165 virtual ~Setup()
166 {
167 std::ostringstream os;
168 destroy(os);
169 }
170
171 private:
172 enum
173 { // add_del_route flags
174 R_IPv6 = (1 << 0),
175 R_IFACE = (1 << 1),
176 R_IFACE_HINT = (1 << 2),
177 R_ONLINK = (1 << 3),
178 R_REJECT = (1 << 4),
179 R_BLACKHOLE = (1 << 5),
180 };
181
182 static void add_del_route(const std::string &addr_str,
183 const int prefix_len,
184 const std::string &gateway_str,
185 const std::string &iface,
186 const unsigned int flags,
187 Action::Ptr &create,
189 {
190 if (flags & R_IPv6)
191 {
192 const IPv6::Addr addr = IPv6::Addr::from_string(addr_str);
194 const IPv6::Addr net = addr & netmask;
195
197 add->argv.push_back("/sbin/route");
198 add->argv.push_back("add");
199 add->argv.push_back("-net");
200 add->argv.push_back("-inet6");
201 add->argv.push_back(net.to_string());
202 add->argv.push_back("-prefixlen");
203 add->argv.push_back(to_string(prefix_len));
204 if (flags & R_REJECT)
205 add->argv.push_back("-reject");
206 if (flags & R_BLACKHOLE)
207 add->argv.push_back("-blackhole");
208 if (!iface.empty())
209 {
210 if (flags & R_IFACE)
211 {
212 add->argv.push_back("-iface");
213 add->argv.push_back(iface);
214 }
215 }
216 if (!gateway_str.empty() && !(flags & R_IFACE))
217 {
218 std::string g = gateway_str;
219 if (flags & R_IFACE_HINT)
220 g += '%' + iface;
221 add->argv.push_back(g);
222 }
223 add->mark = net.to_string();
224 create = add;
225
226 // for the destroy command, copy the add command but replace "add" with "delete"
227 Command::Ptr del(add->copy());
228 del->argv[1] = "delete";
229 destroy = del;
230 }
231 else
232 {
233 const IPv4::Addr addr = IPv4::Addr::from_string(addr_str);
235 const IPv4::Addr net = addr & netmask;
236
238 add->argv.push_back("/sbin/route");
239 add->argv.push_back("add");
240 if (flags & R_ONLINK)
241 {
242 add->argv.push_back("-cloning");
243 add->argv.push_back("-net");
244 add->argv.push_back(net.to_string());
245 add->argv.push_back("-netmask");
246 add->argv.push_back(netmask.to_string());
247 add->argv.push_back("-interface");
248 add->argv.push_back(iface);
249 }
250 else
251 {
252 add->argv.push_back("-net");
253 add->argv.push_back(net.to_string());
254 add->argv.push_back("-netmask");
255 add->argv.push_back(netmask.to_string());
256 if (flags & R_REJECT)
257 add->argv.push_back("-reject");
258 if (flags & R_BLACKHOLE)
259 add->argv.push_back("-blackhole");
260 if (!iface.empty())
261 {
262 if (flags & R_IFACE)
263 {
264 add->argv.push_back("-iface");
265 add->argv.push_back(iface);
266 }
267 }
268 add->argv.push_back(gateway_str);
269 }
270 add->mark = net.to_string();
271 create = add;
272
273 // for the destroy command, copy the add command but replace "add" with "delete"
274 Command::Ptr del(add->copy());
275 del->argv[1] = "delete";
276 destroy = del;
277 }
278 }
279
280 static void add_del_route(const std::string &addr_str,
281 const int prefix_len,
282 const std::string &gateway_str,
283 const std::string &iface,
284 const unsigned int flags,
285 ActionList &create,
287 {
288 Action::Ptr c, d;
289 add_del_route(addr_str, prefix_len, gateway_str, iface, flags, c, d);
290 create.add(c);
291 destroy.add(d);
292 }
293
294 static void tun_config(const std::string &iface_name,
295 const TunBuilderCapture &pull,
296 ActionList &create,
298 std::ostream &os)
299 {
300 // set local4 and local6 to point to IPv4/6 route configurations
301 const TunBuilderCapture::RouteAddress *local4 = nullptr;
302 const TunBuilderCapture::RouteAddress *local6 = nullptr;
303 if (pull.tunnel_address_index_ipv4 >= 0)
304 local4 = &pull.tunnel_addresses[pull.tunnel_address_index_ipv4];
305 if (pull.tunnel_address_index_ipv6 >= 0)
306 local6 = &pull.tunnel_addresses[pull.tunnel_address_index_ipv6];
307
308 // Interface down
309 Command::Ptr iface_down(new Command);
310 iface_down->argv.push_back("/sbin/ifconfig");
311 iface_down->argv.push_back(iface_name);
312 iface_down->argv.push_back("down");
313 create.add(iface_down);
314
315 // Set IPv4 Interface
316 if (local4)
317 {
318 // Process ifconfig
320 {
321 Command::Ptr cmd(new Command);
322 cmd->argv.push_back("/sbin/ifconfig");
323 cmd->argv.push_back(iface_name);
324 cmd->argv.push_back(local4->address);
325 cmd->argv.push_back(local4->gateway);
326 cmd->argv.push_back("netmask");
327 cmd->argv.push_back(netmask.to_string());
328 cmd->argv.push_back("mtu");
329 cmd->argv.push_back(to_string(pull.mtu));
330 cmd->argv.push_back("up");
331 create.add(cmd);
332 }
333 add_del_route(local4->address, local4->prefix_length, local4->address, iface_name, 0, create, destroy);
334 }
335
336 // Set IPv6 Interface
337 if (local6 && !pull.block_ipv6)
338 {
339 {
340 Command::Ptr cmd(new Command);
341 cmd->argv.push_back("/sbin/ifconfig");
342 cmd->argv.push_back(iface_name);
343 cmd->argv.push_back("inet6");
344 cmd->argv.push_back(local6->address + '/' + to_string(local6->prefix_length));
345 cmd->argv.push_back("up");
346 create.add(cmd);
347 }
348 add_del_route(local6->address, local6->prefix_length, "", iface_name, R_IPv6 | R_IFACE, create, destroy);
349 }
350
351 // Process Routes
352 {
353 for (std::vector<TunBuilderCapture::Route>::const_iterator i = pull.add_routes.begin(); i != pull.add_routes.end(); ++i)
354 {
355 const TunBuilderCapture::Route &route = *i;
356 if (route.ipv6)
357 {
358 if (!pull.block_ipv6)
359 add_del_route(route.address, route.prefix_length, local6->gateway, iface_name, R_IPv6 | R_IFACE, create, destroy);
360 }
361 else
362 {
363 if (local4 && !local4->gateway.empty())
364 add_del_route(route.address, route.prefix_length, local4->gateway, iface_name, 0, create, destroy);
365 else
366 os << "ERROR: IPv4 route pushed without IPv4 ifconfig and/or route-gateway" << std::endl;
367 }
368 }
369 }
370
371 // Process exclude routes
372 if (!pull.exclude_routes.empty())
373 {
374 // get default gateways
377
378 for (std::vector<TunBuilderCapture::Route>::const_iterator i = pull.exclude_routes.begin(); i != pull.exclude_routes.end(); ++i)
379 {
380 const TunBuilderCapture::Route &route = *i;
381 if (route.ipv6)
382 {
383 if (!pull.block_ipv6)
384 {
385 if (gw6.flags() & MacGatewayInfo::ADDR_DEFINED)
386 {
387 // MacGatewayInfo already includes interface hint so no need to specify R_IFACE_HINT here
388 add_del_route(route.address, route.prefix_length, gw6.gateway_addr_str(), gw6.iface(), R_IPv6, create, destroy);
389 }
390 else
391 os << "NOTE: cannot determine gateway for exclude IPv6 routes" << std::endl;
392 }
393 }
394 else
395 {
396 if (gw4.flags() & MacGatewayInfo::ADDR_DEFINED)
397 add_del_route(route.address, route.prefix_length, gw4.gateway_addr_str(), gw4.iface(), 0, create, destroy);
398 else
399 os << "NOTE: cannot determine gateway for exclude IPv4 routes" << std::endl;
400 }
401 }
402 }
403
404 // Process IPv4 redirect-gateway
405 if (pull.reroute_gw.ipv4)
406 {
407 // add server bypass route if remote is also IPv4
408 if (!pull.remote_address.ipv6)
409 {
411 if (gw4.flags() & MacGatewayInfo::ADDR_DEFINED)
412 {
414 {
415 Action::Ptr c, d;
416 add_del_route(pull.remote_address.address, 32, gw4.gateway_addr_str(), gw4.iface(), 0, c, d);
417 create.add(c);
418 destroy.add(d);
419 }
420 }
421 else
422 {
423 os << "ERROR: cannot detect IPv4 default gateway" << std::endl;
424 }
425 }
426 else
427 {
428 os << "remote is IPv6, skip bypass route for reroute-ipv4" << std::endl;
429 }
430
432 {
433 add_del_route("0.0.0.0", 1, local4->gateway, iface_name, 0, create, destroy);
434 add_del_route("128.0.0.0", 1, local4->gateway, iface_name, 0, create, destroy);
435 }
436 }
437
438 // Process IPv6 redirect-gateway
439 if (pull.reroute_gw.ipv6 && !pull.block_ipv6)
440 {
441 // add server bypass route if remote is also ipv6
442 if (pull.remote_address.ipv6)
443 {
445 if (gw6.flags() & MacGatewayInfo::ADDR_DEFINED)
446 {
448 {
449 Action::Ptr c, d;
450 // MacGatewayInfo already includes interface hint so no need to specify R_IFACE_HINT here
451 add_del_route(pull.remote_address.address, 128, gw6.gateway_addr_str(), gw6.iface(), R_IPv6, c, d);
452 create.add(c);
453 destroy.add(d);
454 }
455 }
456 else
457 {
458 os << "ERROR: cannot detect IPv6 default gateway" << std::endl;
459 }
460 }
461 else
462 {
463 os << "remote is IPv4, skip bypass route for reroute-ipv6" << std::endl;
464 }
465
467 {
468 add_del_route("0000::", 1, local6->gateway, iface_name, R_IPv6 | R_IFACE, create, destroy);
469 add_del_route("8000::", 1, local6->gateway, iface_name, R_IPv6 | R_IFACE, create, destroy);
470 }
471 }
472
473 // Process block-ipv6
474 if (pull.block_ipv6)
475 {
476 add_del_route("2000::", 4, "::1", "lo0", R_IPv6 | R_REJECT | R_IFACE_HINT, create, destroy);
477 add_del_route("3000::", 4, "::1", "lo0", R_IPv6 | R_REJECT | R_IFACE_HINT, create, destroy);
478 add_del_route("fc00::", 7, "::1", "lo0", R_IPv6 | R_REJECT | R_IFACE_HINT, create, destroy);
479 }
480
481 // Interface down
482 destroy.add(iface_down);
483
484 // configure DNS
485 {
486 MacDNS::Config::Ptr dns(new MacDNS::Config(pull));
489#ifdef ENABLE_DNS_WATCHDOG
492#endif
493 ,
494 create,
495 destroy);
496 }
497
499 ProxySettings::add_actions<MacProxySettings>(pull, create, destroy);
500 }
501
503
504 public:
505 static void add_bypass_route(const std::string &route,
506 bool ipv6,
507 ActionList &add_cmds,
508 ActionList &remove_cmds_bypass_gw)
509 {
510 MacGatewayInfo gw{IP::Addr{route}};
511 if (gw.flags() & MacGatewayInfo::ADDR_DEFINED)
512 {
513 if (!ipv6)
514 {
515 add_del_route(route, 32, gw.gateway_addr_str(), gw.iface(), 0, add_cmds, remove_cmds_bypass_gw);
516 }
517 else
518 {
519 // MacGatewayInfo already includes interface hint so no need to specify R_IFACE_HINT here
520 add_del_route(route, 128, gw.gateway_addr_str(), gw.iface(), R_IPv6, add_cmds, remove_cmds_bypass_gw);
521 }
522 }
523 }
524};
525} // namespace openvpn::TunMac
526
527#endif
void destroy(std::ostream &os) override
Definition action.hpp:151
void add(Action *action)
Definition action.hpp:57
virtual std::unordered_set< std::string > execute(std::ostream &os)
Executes a sequence of actions and returns marks of failed actions.
Definition action.hpp:98
void remove_marked(const std::unordered_set< std::string > &marks, std::ostream &os)
Removes actions with specified marks and logs the removals.
Definition action.hpp:175
void enable_destroy(const bool state)
Definition action.hpp:141
static Addr from_ipv6(IPv6::Addr addr)
Definition ip.hpp:268
static Addr from_ipv4(IPv4::Addr addr)
Definition ip.hpp:260
static Addr netmask_from_prefix_len(const unsigned int prefix_len)
Definition ipv4.hpp:189
static Addr from_zero()
Definition ipv4.hpp:168
std::string to_string() const
Definition ipv4.hpp:232
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv4.hpp:205
static Addr netmask_from_prefix_len(const unsigned int prefix_len)
Definition ipv6.hpp:328
static Addr from_zero()
Definition ipv6.hpp:307
std::string to_string() const
Definition ipv6.hpp:131
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv6.hpp:104
const char * str() const
Definition layer.hpp:61
static Layer from_str(const std::string &str)
Definition layer.hpp:91
static void add_actions(const MacDNS::Config::Ptr &dns, const unsigned int flags, ActionList &create, ActionList &destroy)
The smart pointer class.
Definition rc.hpp:119
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
Definition rc.hpp:290
bool defined() const
Checks if the URL is defined.
Definition capture.hpp:421
Route address class that may use non-canonical form.
Definition capture.hpp:295
Route class that must use canonical form.
Definition capture.hpp:313
RemoteAddress remote_address
Definition capture.hpp:1082
std::vector< RouteAddress > tunnel_addresses
Definition capture.hpp:1083
std::vector< Route > add_routes
Definition capture.hpp:1091
ProxyAutoConfigURL proxy_auto_config_url
Definition capture.hpp:1096
std::vector< Route > exclude_routes
Definition capture.hpp:1092
RCPtr< Setup > Ptr
Definition tunsetup.hpp:47
int establish(const TunBuilderCapture &pull, TunBuilderSetup::Config *config, Stop *stop, std::ostream &os) override
Definition tunsetup.hpp:86
static void add_bypass_route(const std::string &route, bool ipv6, ActionList &add_cmds, ActionList &remove_cmds_bypass_gw)
Definition tunsetup.hpp:505
ActionList::Ptr remove_cmds
Definition tunsetup.hpp:502
static void add_del_route(const std::string &addr_str, const int prefix_len, const std::string &gateway_str, const std::string &iface, const unsigned int flags, ActionList &create, ActionList &destroy)
Definition tunsetup.hpp:280
static void add_del_route(const std::string &addr_str, const int prefix_len, const std::string &gateway_str, const std::string &iface, const unsigned int flags, Action::Ptr &create, Action::Ptr &destroy)
Definition tunsetup.hpp:182
bool add_bypass_route(const std::string &address, bool ipv6, std::ostream &os)
Definition tunsetup.hpp:78
OPENVPN_EXCEPTION(tun_mac_setup)
static void tun_config(const std::string &iface_name, const TunBuilderCapture &pull, ActionList &create, ActionList &destroy, std::ostream &os)
Definition tunsetup.hpp:294
void destroy(std::ostream &os) override
Definition tunsetup.hpp:156
int utun_open(std::string &name, const int unit)
Definition utun.hpp:48
int tuntap_open(const Layer &layer, std::string &name)
Definition tunutil.hpp:32
void assert_dict(const Json::Value &obj, const TITLE &title)
void to_string(const Json::Value &root, std::string &dest, const NAME &name, const TITLE &title)
void to_bool(const Json::Value &root, bool &dest, const NAME &name, const TITLE &title)
std::string get_string(const Json::Value &root, const NAME &name, const TITLE &title)
void from_json(const Json::Value &root, const std::string &title) override
Definition tunsetup.hpp:68
Json::Value to_json() override
Definition tunsetup.hpp:59
reroute_gw flags
remote_address ipv6
remote_address address
std::ostringstream os
auto g(const ThingT &t)
static const char config[]
int prefix_len(const IPv4::Addr::base_type mask)
static void add(const Time &t1, const Time::Duration &d1)