OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tunnetlink.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#pragma once
13
14#include <sys/ioctl.h>
15#include <fcntl.h>
16#include <net/if.h>
17#include <linux/if_tun.h>
18
28
29namespace openvpn::TunNetlink {
30
31using namespace openvpn::TunLinuxSetup;
32
33struct NetlinkLinkSet : public Action
34{
36
37 NetlinkLinkSet() = default;
38
39 NetlinkLinkSet(std::string dev_arg, bool up_arg, int mtu_arg)
40 : dev(dev_arg),
41 up(up_arg),
42 mtu(mtu_arg)
43 {
44 }
45
47 {
49 ret->dev = dev;
50 ret->up = up;
51 ret->mtu = mtu;
52 return ret;
53 }
54
55 void execute(std::ostream &os) override
56 {
57 if (dev.empty())
58 {
59 os << "Error: can't call NetlinkLinkSet with no interface\n";
60 return;
61 }
62
64 if (ret)
65 {
66 os << "Error while executing NetlinkLinkSet " << dev << " mtu " << mtu
67 << ": " << ret << "\n";
68 }
69
71 if (ret)
72 {
73 os << "Error while executing NetlinkLinkSet " << dev << " up " << up
74 << ": " << ret << "\n";
75 }
76 }
77
78 std::string to_string() const override
79 {
80 std::ostringstream os;
81 os << "netlink iface " << dev << " link set " << up << " mtu " << mtu;
82 return os.str();
83 }
84
85 std::string dev;
86 bool up = true;
88};
89
90struct NetlinkAddr4 : public Action
91{
93
94 NetlinkAddr4() = default;
95
96 NetlinkAddr4(std::string dev_arg,
97 IPv4::Addr &addr_arg,
98 unsigned char prefixlen_arg,
99 IPv4::Addr &broadcast_arg,
100 bool add_arg)
101 : dev(dev_arg),
102 addr(addr_arg),
103 prefixlen(prefixlen_arg),
104 broadcast(broadcast_arg),
105 add(add_arg)
106 {
107 }
108
110 {
112 ret->dev = dev;
113 ret->addr = addr;
114 ret->prefixlen = prefixlen;
115 ret->broadcast = broadcast;
116 ret->add = add;
117 return ret;
118 }
119
120 void execute(std::ostream &os) override
121 {
122 if (dev.empty())
123 {
124 os << "Error: can't call NetlinkAddr4 with no interface\n";
125 return;
126 }
127
128 int ret;
129 if (add)
130 {
132 }
133 else
134 {
136 }
137
138 if (ret)
139 {
140 os << "Error while executing NetlinkAddr4(add: " << add << ") "
141 << dev << ": " << ret << "\n";
142 }
143 }
144
145 std::string to_string() const override
146 {
147 std::ostringstream os;
148 os << "netlink iface " << dev << " " << (add ? "add" : "del") << " "
149 << addr.to_string() << "/" << prefixlen << " broadcast "
150 << broadcast.to_string();
151 return os.str();
152 }
153
154 std::string dev;
156 unsigned char prefixlen = 0;
158 bool add = true;
159};
160
161struct NetlinkAddr6 : public Action
162{
164
165 NetlinkAddr6() = default;
166
167 NetlinkAddr6(std::string dev_arg,
168 IPv6::Addr &addr_arg,
169 unsigned char prefixlen_arg,
170 bool add_arg)
171 : dev(dev_arg),
172 addr(addr_arg),
173 prefixlen(prefixlen_arg),
174 add(add_arg)
175 {
176 }
177
179 {
181 ret->dev = dev;
182 ret->addr = addr;
183 ret->prefixlen = prefixlen;
184 ret->add = add;
185 return ret;
186 }
187
188 void execute(std::ostream &os) override
189 {
190 if (dev.empty())
191 {
192 os << "Error: can't call NetlinkAddr6 with no interface\n";
193 return;
194 }
195
196 int ret;
197 if (add)
198 {
200 }
201 else
202 {
204 }
205
206 if (ret)
207 {
208 os << "Error while executing NetlinkAddr6(add: " << add << ") "
209 << dev << ": " << ret << "\n";
210 }
211 }
212
213 std::string to_string() const override
214 {
215 std::ostringstream os;
216 os << "netlink iface " << dev << " " << (add ? "add" : "del") << " "
217 << addr.to_string() << "/" << prefixlen;
218 return os.str();
219 }
220
221 std::string dev;
223 unsigned char prefixlen = 0;
224 bool add = true;
225};
226
227struct NetlinkAddr4PtP : public Action
228{
230
231 NetlinkAddr4PtP() = default;
232
233 NetlinkAddr4PtP(std::string dev_arg,
234 IPv4::Addr local_arg,
235 IPv4::Addr remote_arg,
236 bool add_arg)
237 : dev(dev_arg),
238 local(local_arg),
239 remote(remote_arg),
240 add(add_arg)
241 {
242 }
243
245 {
247 ret->dev = dev;
248 ret->local = local;
249 ret->remote = remote;
250 ret->add = add;
251 return ret;
252 }
253
254 void execute(std::ostream &os) override
255 {
256 if (dev.empty())
257 {
258 os << "Error: can't call NetlinkAddr4PtP with no interface\n";
259 return;
260 }
261
262 int ret;
263 if (add)
264 {
266 }
267 else
268 {
270 }
271
272 if (ret)
273 {
274 os << "Error while executing NetlinkAddr4PtP(add: " << add << ") "
275 << dev << ": " << ret << "\n";
276 }
277 }
278
279 std::string to_string() const override
280 {
281 return "netlink iface " + dev + " " + (add ? "add" : "del") + " ptp "
282 + local.to_string() + " remote " + remote.to_string();
283 }
284
285 std::string dev;
288 bool add = true;
289};
290
291struct NetlinkRoute4 : public Action
292{
294
295 NetlinkRoute4() = default;
296
298 int prefixlen_arg,
299 IPv4::Addr &gw_arg,
300 std::string dev_arg,
301 int metric_arg,
302 bool add_arg)
303 : route(dst_arg, prefixlen_arg),
304 gw(gw_arg),
305 dev(dev_arg),
306 metric(metric_arg),
307 add(add_arg)
308 {
309 }
310
312 {
314 ret->route = route;
315 ret->gw = gw;
316 ret->dev = dev;
317 ret->metric = metric;
318 return ret;
319 }
320
321 void execute(std::ostream &os) override
322 {
323 if (dev.empty())
324 {
325 os << "Error: can't call NetlinkRoute4 with no interface\n";
326 return;
327 }
328
329 int ret;
330 if (add)
331 {
333 }
334 else
335 {
337 }
338
339 if (ret)
340 {
341 os << "Error while executing NetlinkRoute4(add: " << add << ") "
342 << dev << ": " << ret << "\n";
343 }
344 }
345
346 std::string to_string() const override
347 {
348 std::ostringstream os;
349 os << "netlink route " << (add ? "add" : "del") << " dev " << dev << " "
350 << route << " via " << gw.to_string() << " metric " << metric;
351 return os.str();
352 }
353
356 std::string dev;
357 int metric = -1;
358 bool add = true;
359};
360
361struct NetlinkRoute6 : public Action
362{
364
365 NetlinkRoute6() = default;
366
368 int prefixlen_arg,
369 IPv6::Addr &gw_arg,
370 std::string dev_arg,
371 int metric_arg,
372 bool add_arg)
373 : route(dst_arg, prefixlen_arg),
374 gw(gw_arg),
375 dev(dev_arg),
376 metric(metric_arg),
377 add(add_arg)
378 {
379 }
380
382 {
384 ret->route = route;
385 ret->gw = gw;
386 ret->dev = dev;
387 ret->metric = metric;
388 return ret;
389 }
390
391 void execute(std::ostream &os) override
392 {
393 if (dev.empty())
394 {
395 os << "Error: can't call NetlinkRoute6 with no interface\n";
396 return;
397 }
398
399 int ret;
400 if (add)
401 {
403 }
404 else
405 {
407 }
408
409 if (ret)
410 {
411 os << "Error while executing NetlinkRoute6(add: " << add << ") "
412 << dev << ": " << ret << "\n";
413 }
414 }
415
416 std::string to_string() const override
417 {
418 std::ostringstream os;
419 os << "netlink route " << (add ? "add" : "del") << " dev " << dev << " "
420 << route << " via " << gw.to_string() << " metric " << metric;
421 return os.str();
422 }
423
426 std::string dev;
427 int metric = -1;
428 bool add = true;
429};
430
431enum
432{ // add_del_route flags
433 R_IPv6 = (1 << 0),
434 R_ADD_SYS = (1 << 1),
435 R_ADD_DCO = (1 << 2),
437};
438
447inline int iface_new(std::ostringstream &os, const std::string &dev, const std::string &type)
448{
449 int ret = -1;
450
451 if (dev.empty())
452 {
453 os << "Error: can't call NetlinkLinkNew with no interface\n";
454 return ret;
455 }
456
457 if (type.empty())
458 {
459 os << "Error: can't call NetlinkLinkNew with no interfacei type\n";
460 return ret;
461 }
462
463 ret = SITNL::net_iface_new(dev, type);
464 if (ret)
465 {
466 os << "Error while executing NetlinkLinkNew " << dev << ": " << ret << "\n";
467 }
468
469 return ret;
470}
471
472inline int iface_del(std::ostringstream &os, const std::string &dev)
473{
474 int ret = -1;
475
476 if (dev.empty())
477 {
478 os << "Error: can't call NetlinkLinkDel with no interface\n";
479 return ret;
480 }
481
483 if (ret)
484 {
485 os << "Error while executing NetlinkLinkDel " << dev << ": " << ret << "\n";
486 }
487
488 return ret;
489}
490
491/*inline IPv4::Addr cvt_pnr_ip_v4(const std::string& hexaddr)
492{
493 BufferAllocated v(4, BufAllocFlags::CONSTRUCT_ZERO);
494 parse_hex(v, hexaddr);
495 if (v.size() != 4)
496throw tun_linux_error("bad hex address");
497 IPv4::Addr ret = IPv4::Addr::from_bytes(v.data());
498 return IP::Addr::from_ipv4(ret);
499}*/
500
501inline void add_del_route(const std::string &addr_str,
502 const int prefix_len,
503 const std::string &gateway_str,
504 const std::string &dev,
505 const int metric,
506 const unsigned int flags,
507 std::vector<IP::Route> *rtvec,
508 Action::Ptr &create,
509 Action::Ptr &destroy)
510{
511 if (flags & R_IPv6)
512 {
513 const IPv6::Addr addr = IPv6::Addr::from_string(addr_str);
515 const IPv6::Addr net = addr & netmask;
516
517 if (flags & R_ADD_SYS)
518 {
519 // ip route add 2001:db8:1::/48 via 2001:db8:1::1
521 add->route.addr = net;
522 add->route.prefix_len = prefix_len;
523 add->gw = IPv6::Addr::from_string(gateway_str);
524 add->dev = dev;
525 add->metric = metric;
526 add->add = true;
527
528 create = add;
529 // for the destroy command, copy the add command but replace "add" with "delete"
530 const NetlinkRoute6::Ptr del(add->copy());
531 del->add = false;
532 destroy = del;
533 }
534
535 if (rtvec && (flags & R_ADD_DCO))
536 rtvec->emplace_back(IP::Addr::from_ipv6(net), prefix_len);
537 }
538 else
539 {
540 const IPv4::Addr addr = IPv4::Addr::from_string(addr_str);
542 const IPv4::Addr net = addr & netmask;
543
544 if (flags & R_ADD_SYS)
545 {
546 // ip route add 192.0.2.128/25 via 192.0.2.1
548 add->route.addr = net;
549 add->route.prefix_len = prefix_len;
550 add->gw = IPv4::Addr::from_string(gateway_str);
551 add->dev = dev;
552 add->metric = metric;
553 add->add = true;
554
555 create = add;
556 // for the destroy command, copy the add command but replace "add" with "delete"
557 const NetlinkRoute4::Ptr del(add->copy());
558 del->add = false;
559 destroy = del;
560 }
561
562 if (rtvec && (flags & R_ADD_DCO))
563 rtvec->emplace_back(IP::Addr::from_ipv4(net), prefix_len);
564 }
565}
566
567inline void add_del_route(const std::string &addr_str,
568 const int prefix_len,
569 const std::string &gateway_str,
570 const std::string &dev,
571 const int metric,
572 const unsigned int flags, // add interface route to rtvec if defined
573 std::vector<IP::Route> *rtvec,
574 ActionList &create,
575 ActionList &destroy)
576{
577 Action::Ptr c, d;
578 add_del_route(addr_str, prefix_len, gateway_str, dev, metric, flags, rtvec, c, d);
579 create.add(c);
580 destroy.add(d);
581}
582
583inline void iface_up(const std::string &iface_name,
584 const int mtu,
585 ActionList &create,
586 ActionList &destroy)
587{
588 {
590 add->dev = iface_name;
591 add->up = true;
592 add->mtu = mtu;
593
594 create.add(add);
595 // for the destroy command, copy the add command but replace "up" with "down"
596 const NetlinkLinkSet::Ptr del(add->copy());
597 del->up = false;
598 destroy.add(del);
599 }
600}
601
602inline void iface_config(const std::string &iface_name,
603 int unit,
604 const TunBuilderCapture &pull,
605 std::vector<IP::Route> *rtvec,
606 ActionList &create,
607 ActionList &destroy)
608{
609 // set local4 and local6 to point to IPv4/6 route configurations
610 const TunBuilderCapture::RouteAddress *local4 = pull.vpn_ipv4();
611 const TunBuilderCapture::RouteAddress *local6 = pull.vpn_ipv6();
612
613 // Set IPv4 Interface
614 if (local4)
615 {
617 add->addr = IPv4::Addr::from_string(local4->address);
618 add->prefixlen = local4->prefix_length;
619 add->broadcast = IPv4::Addr::from_string(local4->address)
620 | ~IPv4::Addr::netmask_from_prefix_len(local4->prefix_length);
621 add->dev = iface_name;
622 add->add = true;
623 // if (unit >= 0)
624 // {
625 // add->argv.push_back("label");
626 // add->argv.push_back(iface_name + ':' + openvpn::to_string(unit));
627 // }
628 create.add(add);
629
630 // for the destroy command, copy the add command but replace "add" with "delete"
631 const NetlinkAddr4::Ptr del(add->copy());
632 del->add = false;
633 destroy.add(del);
634
635 // add interface route to rtvec if defined
636 add_del_route(local4->address,
637 local4->prefix_length,
638 local4->address,
639 iface_name,
640 0,
641 R_ADD_DCO,
642 rtvec,
643 create,
644 destroy);
645 }
646
647 // Set IPv6 Interface
648 if (local6 && !pull.block_ipv6)
649 {
651 add->addr = IPv6::Addr::from_string(local6->address);
652 add->prefixlen = local6->prefix_length;
653 add->dev = iface_name;
654 add->add = true;
655
656 create.add(add);
657
658 // for the destroy command, copy the add command but replace "add" with "delete"
659 const NetlinkAddr6::Ptr del(add->copy());
660 del->add = false;
661 destroy.add(del);
662
663 // add interface route to rtvec if defined
664 add_del_route(local6->address,
665 local6->prefix_length,
666 local6->address,
667 iface_name,
668 0,
670 rtvec,
671 create,
672 destroy);
673 }
674}
675
677{
678 static inline void tun_config(const std::string &iface_name,
679 const TunBuilderCapture &pull,
680 std::vector<IP::Route> *rtvec,
681 ActionList &create,
682 ActionList &destroy,
683 const unsigned int flags) // TunConfigFlags
684 {
685 // set local4 and local6 to point to IPv4/6 route configurations
686 const TunBuilderCapture::RouteAddress *local4 = pull.vpn_ipv4();
687 const TunBuilderCapture::RouteAddress *local6 = pull.vpn_ipv6();
688
689 // configure interface
691 iface_up(iface_name, pull.mtu, create, destroy);
692 iface_config(iface_name, -1, pull, rtvec, create, destroy);
693
694 // Process Routes
695 {
696 for (const auto &route : pull.add_routes)
697 {
698 if (route.ipv6)
699 {
700 if (local6 && !pull.block_ipv6)
701 add_del_route(route.address,
702 route.prefix_length,
703 local6->gateway,
704 iface_name,
705 route.metric,
707 rtvec,
708 create,
709 destroy);
710 }
711 else
712 {
713 if (local4 && !local4->gateway.empty())
714 add_del_route(route.address,
715 route.prefix_length,
716 local4->gateway,
717 iface_name,
718 route.metric,
719 R_ADD_ALL,
720 rtvec,
721 create,
722 destroy);
723 else
724 OPENVPN_LOG("ERROR: IPv4 route pushed without IPv4 ifconfig and/or route-gateway");
725 }
726 }
727 }
728
729 // Process exclude routes
730 if (!pull.exclude_routes.empty())
731 {
732 const LinuxGW46Netlink gw(iface_name);
733
734 for (const auto &route : pull.exclude_routes)
735 {
736 if (route.ipv6)
737 {
738 OPENVPN_LOG("NOTE: exclude IPv6 routes not supported yet"); // fixme
739 }
740 else
741 {
742 if (gw.v4.defined())
743 add_del_route(route.address,
744 route.prefix_length,
745 gw.v4.addr().to_string(),
746 gw.v4.dev(),
747 route.metric,
748 R_ADD_SYS,
749 rtvec,
750 create,
751 destroy);
752 else
753 OPENVPN_LOG("NOTE: cannot determine gateway for exclude IPv4 routes");
754 }
755 }
756 }
757
758 // Process IPv4 redirect-gateway
760 {
761 if (pull.reroute_gw.ipv4 && local4)
762 {
763 // add bypass route
765 && !pull.remote_address.ipv6
767 {
768 add_bypass_route(iface_name,
770 false,
771 rtvec,
772 create,
773 destroy);
774 }
775
776 add_del_route("0.0.0.0", 1, local4->gateway, iface_name, 0, R_ADD_ALL, rtvec, create, destroy);
777 add_del_route("128.0.0.0", 1, local4->gateway, iface_name, 0, R_ADD_ALL, rtvec, create, destroy);
778 }
779
780 // Process IPv6 redirect-gateway
781 if (pull.reroute_gw.ipv6 && !pull.block_ipv6 && local6)
782 {
783 // add bypass route
785 && pull.remote_address.ipv6
787 {
788 add_bypass_route(iface_name,
790 true,
791 rtvec,
792 create,
793 destroy);
794 }
795
796 add_del_route("0000::", 1, local6->gateway, iface_name, 0, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
797 add_del_route("8000::", 1, local6->gateway, iface_name, 0, R_ADD_ALL | R_IPv6, rtvec, create, destroy);
798 }
799 }
800
801 // fixme -- Process block-ipv6
802
803 // fixme -- Handle pushed DNS servers
804 }
805
806 static inline void add_bypass_route(const std::string &tun_iface_name,
807 const std::string &address,
808 bool ipv6,
809 std::vector<IP::Route> *rtvec,
810 ActionList &create,
811 ActionList &destroy)
812 {
813 const LinuxGW46Netlink gw(tun_iface_name, address);
814
815 if (!ipv6 && gw.v4.defined())
817 32,
818 gw.v4.addr().to_string(),
819 gw.dev(),
820 0,
821 R_ADD_SYS,
822 rtvec,
823 create,
824 destroy);
825
826 if (ipv6 && gw.v6.defined())
828 128,
829 gw.v6.addr().to_string(),
830 gw.dev(),
831 0,
833 rtvec,
834 create,
835 destroy);
836 }
837};
838} // namespace openvpn::TunNetlink
void add(Action *action)
Definition action.hpp:57
std::string to_string() const
Definition ip.hpp:528
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:186
unsigned int prefix_len() const
Definition ipv4.hpp:453
std::string to_string() const
Definition ipv4.hpp:229
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv4.hpp:202
unsigned int prefix_len() const
Definition ipv6.hpp:523
static Addr netmask_from_prefix_len(const unsigned int prefix_len)
Definition ipv6.hpp:325
static void add(ipv6addr &dest, const ipv6addr &src)
Definition ipv6.hpp:861
std::string to_string() const
Definition ipv6.hpp:128
static Addr from_string(const std::string &ipstr, const TITLE &title)
Definition ipv6.hpp:101
The smart pointer class.
Definition rc.hpp:119
Route address class that may use non-canonical form.
Definition capture.hpp:295
RemoteAddress remote_address
Definition capture.hpp:1082
std::vector< Route > add_routes
Definition capture.hpp:1091
const RouteAddress * vpn_ipv6() const
Gets the IPv6 tunnel address.
Definition capture.hpp:914
const RouteAddress * vpn_ipv4() const
Gets the IPv4 tunnel address.
Definition capture.hpp:902
std::vector< Route > exclude_routes
Definition capture.hpp:1092
#define OPENVPN_LOG(args)
@ TUN_MTU_DEFAULT
Definition tunmtu.hpp:20
const auto metric
reroute_gw flags
remote_address ipv6
std::string ret
remote_address address
std::ostringstream os
int prefix_len(const IPv4::Addr::base_type mask)
static void add(const Time &t1, const Time::Duration &d1)