OpenVPN
route.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2002-2026 OpenVPN Inc <sales@openvpn.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23/*
24 * Support routines for adding/deleting network routes.
25 */
26#include <stddef.h>
27#include <stdbool.h>
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34
35#include "common.h"
36#include "error.h"
37#include "route.h"
38#include "run_command.h"
39#include "socket.h"
40#include "manage.h"
41#include "win32.h"
42#include "options.h"
43#include "networking.h"
44#include "integer.h"
45#include "resolve.h"
46
47#include "memdbg.h"
48
49#if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
50#include <linux/rtnetlink.h> /* RTM_GETROUTE etc. */
51#endif
52
53#if defined(TARGET_NETBSD)
54#include <net/route.h> /* RT_ROUNDUP(), RT_ADVANCE() */
55#endif
56
57#ifdef _WIN32
58#include "openvpn-msg.h"
59
60#define METRIC_NOT_USED ((DWORD)-1)
61static int add_route_service(const struct route_ipv4 *, const struct tuntap *);
62
63static bool del_route_service(const struct route_ipv4 *, const struct tuntap *);
64
65static int add_route_ipv6_service(const struct route_ipv6 *, const struct tuntap *);
66
67static bool del_route_ipv6_service(const struct route_ipv6 *, const struct tuntap *);
68
69static int route_ipv6_ipapi(bool add, const struct route_ipv6 *, const struct tuntap *);
70
71static int add_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt,
72 DWORD adapter_index);
73
74static bool del_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt);
75
76
77#endif
78
79static void delete_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
80 const struct route_gateway_info *rgi, const struct env_set *es,
82
83static void get_bypass_addresses(struct route_bypass *rb, const unsigned int flags);
84
85#ifndef ENABLE_SMALL
86
87static void
89{
90 struct gc_arena gc = gc_new();
91 int i;
92 for (i = 0; i < rb->n_bypass; ++i)
93 {
94 msg(D_ROUTE, "ROUTE: bypass_host_route[%d]=%s", i, print_in_addr_t(rb->bypass[i], 0, &gc));
95 }
96 gc_free(&gc);
97}
98
99#endif
100
101/* Route addition return status codes */
102#define RTA_ERROR 0 /* route addition failed */
103#define RTA_SUCCESS 1 /* route addition succeeded */
104#define RTA_EEXIST 2 /* route not added as it already exists */
105
106#ifndef TARGET_ANDROID
107static bool
109{
110 int i;
111 for (i = 0; i < rb->n_bypass; ++i)
112 {
113 if (a == rb->bypass[i]) /* avoid duplicates */
114 {
115 return true;
116 }
117 }
118 if (rb->n_bypass < N_ROUTE_BYPASS)
119 {
120 rb->bypass[rb->n_bypass++] = a;
121 return true;
122 }
123 else
124 {
125 return false;
126 }
127}
128#endif
129
130struct route_option_list *
132{
133 struct route_option_list *ret;
135 ret->gc = a;
136 return ret;
137}
138
141{
142 struct route_ipv6_option_list *ret;
144 ret->gc = a;
145 return ret;
146}
147
148/*
149 * NOTE: structs are cloned/copied shallow by design.
150 * The routes list from src will stay intact since it is allocated using
151 * the options->gc. The cloned/copied lists will share this common tail
152 * to avoid copying the data around between pulls. Pulled routes use
153 * the c2->gc so they get freed immediately after a reconnect.
154 */
155struct route_option_list *
157{
158 struct route_option_list *ret;
159 ALLOC_OBJ_GC(ret, struct route_option_list, a);
160 *ret = *src;
161 return ret;
162}
163
166{
167 struct route_ipv6_option_list *ret;
168 ALLOC_OBJ_GC(ret, struct route_ipv6_option_list, a);
169 *ret = *src;
170 return ret;
171}
172
173void
175 struct gc_arena *a)
176{
177 *dest = *src;
178 dest->gc = a;
179}
180
181void
183 const struct route_ipv6_option_list *src, struct gc_arena *a)
184{
185 *dest = *src;
186 dest->gc = a;
187}
188
189static bool
190is_route_parm_defined(const char *parm)
191{
192 if (!parm)
193 {
194 return false;
195 }
196 if (!strcmp(parm, "default"))
197 {
198 return false;
199 }
200 return true;
201}
202
203static void
204setenv_route_addr(struct env_set *es, const char *key, const in_addr_t addr, int i)
205{
206 struct gc_arena gc = gc_new();
207 struct buffer name = alloc_buf_gc(256, &gc);
208 if (i >= 0)
209 {
210 buf_printf(&name, "route_%s_%d", key, i);
211 }
212 else
213 {
214 buf_printf(&name, "route_%s", key);
215 }
216 setenv_str(es, BSTR(&name), print_in_addr_t(addr, 0, &gc));
217 gc_free(&gc);
218}
219
220static bool
221get_special_addr(const struct route_list *rl, const char *string, in_addr_t *out, bool *status)
222{
223 if (status)
224 {
225 *status = true;
226 }
227 if (!strcmp(string, "vpn_gateway"))
228 {
229 if (rl)
230 {
232 {
233 *out = rl->spec.remote_endpoint;
234 }
235 else
236 {
237 msg(M_INFO, PACKAGE_NAME " ROUTE: vpn_gateway undefined");
238 if (status)
239 {
240 *status = false;
241 }
242 }
243 }
244 return true;
245 }
246 else if (!strcmp(string, "net_gateway"))
247 {
248 if (rl)
249 {
250 if (rl->ngi.flags & RGI_ADDR_DEFINED)
251 {
252 *out = rl->ngi.gateway.addr;
253 }
254 else
255 {
256 msg(M_INFO, PACKAGE_NAME
257 " ROUTE: net_gateway undefined -- unable to get default gateway from system");
258 if (status)
259 {
260 *status = false;
261 }
262 }
263 }
264 return true;
265 }
266 else if (!strcmp(string, "remote_host"))
267 {
268 if (rl)
269 {
270 if (rl->spec.flags & RTSA_REMOTE_HOST)
271 {
272 *out = rl->spec.remote_host;
273 }
274 else
275 {
276 msg(M_INFO, PACKAGE_NAME " ROUTE: remote_host undefined");
277 if (status)
278 {
279 *status = false;
280 }
281 }
282 }
283 return true;
284 }
285 return false;
286}
287
288bool
289is_special_addr(const char *addr_str)
290{
291 if (addr_str)
292 {
293 return get_special_addr(NULL, addr_str, NULL, NULL);
294 }
295 else
296 {
297 return false;
298 }
299}
300
301static bool
302init_route(struct route_ipv4 *r, struct addrinfo **network_list, const struct route_option *ro,
303 const struct route_list *rl)
304{
305 const in_addr_t default_netmask = IPV4_NETMASK_HOST;
306 bool status;
307 int ret;
308 struct in_addr special = { 0 };
309
310 CLEAR(*r);
311 r->option = ro;
312
313 /* network */
315 {
316 goto fail;
317 }
318
319 /* get_special_addr replaces specialaddr with a special ip addr
320 * like gw. getaddrinfo is called to convert a a addrinfo struct */
321
322 if (get_special_addr(rl, ro->network, (in_addr_t *)&special.s_addr, &status))
323 {
324 if (!status)
325 {
326 goto fail;
327 }
328 special.s_addr = htonl(special.s_addr);
329 char buf[INET_ADDRSTRLEN];
330 inet_ntop(AF_INET, &special, buf, sizeof(buf));
331 ret = openvpn_getaddrinfo(0, buf, NULL, 0, NULL, AF_INET, network_list);
332 }
333 else
334 {
336 NULL, AF_INET, network_list);
337 }
338
339 status = (ret == 0);
340
341 if (!status)
342 {
343 goto fail;
344 }
345
346 /* netmask */
347
349 {
350 r->netmask =
352 if (!status)
353 {
354 goto fail;
355 }
356 }
357 else
358 {
359 r->netmask = default_netmask;
360 }
361
362 /* gateway */
363
365 {
366 if (!get_special_addr(rl, ro->gateway, &r->gateway, &status))
367 {
369 ro->gateway, 0, &status, NULL);
370 }
371 if (!status)
372 {
373 goto fail;
374 }
375 }
376 else
377 {
379 {
381 }
382 else
383 {
384 msg(M_WARN, PACKAGE_NAME
385 " ROUTE: " PACKAGE_NAME
386 " needs a gateway parameter for a --route option and no default was specified by either --route-gateway or --ifconfig options");
387 goto fail;
388 }
389 }
390
391 /* metric */
392
393 r->metric = 0;
395 {
396 r->metric = atoi(ro->metric);
397 if (r->metric < 0)
398 {
399 msg(M_WARN, PACKAGE_NAME " ROUTE: route metric for network %s (%s) must be >= 0",
400 ro->network, ro->metric);
401 goto fail;
402 }
404 }
405 else if (rl->spec.flags & RTSA_DEFAULT_METRIC)
406 {
407 r->metric = rl->spec.default_metric;
409 }
410
411 r->flags |= RT_DEFINED;
412
413 /* routing table id */
414 r->table_id = ro->table_id;
415
416 return true;
417
418fail:
419 msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route for host/network: %s",
420 ro->network);
421 return false;
422}
423
424static bool
426 const struct route_ipv6_list *rl6)
427{
428 CLEAR(*r6);
429
430 if (!get_ipv6_addr(r6o->prefix, &r6->network, &r6->netbits, M_WARN))
431 {
432 goto fail;
433 }
434
435 /* gateway */
437 {
438 if (inet_pton(AF_INET6, r6o->gateway, &r6->gateway) != 1)
439 {
440 msg(M_WARN, PACKAGE_NAME "ROUTE6: cannot parse gateway spec '%s'", r6o->gateway);
441 }
442 }
443 else if (rl6->spec_flags & RTSA_REMOTE_ENDPOINT)
444 {
445 r6->gateway = rl6->remote_endpoint_ipv6;
446 }
447
448 /* metric */
449
450 r6->metric = -1;
452 {
453 r6->metric = atoi(r6o->metric);
454 if (r6->metric < 0)
455 {
456 msg(M_WARN, PACKAGE_NAME " ROUTE: route metric for network %s (%s) must be >= 0",
457 r6o->prefix, r6o->metric);
458 goto fail;
459 }
460 r6->flags |= RT_METRIC_DEFINED;
461 }
462 else if (rl6->spec_flags & RTSA_DEFAULT_METRIC)
463 {
464 r6->metric = rl6->default_metric;
465 r6->flags |= RT_METRIC_DEFINED;
466 }
467
468 r6->flags |= RT_DEFINED;
469
470 /* routing table id */
471 r6->table_id = r6o->table_id;
472
473 return true;
474
475fail:
476 msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route for host/network: %s",
477 r6o->prefix);
478 return false;
479}
480
481void
482add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask,
483 const char *gateway, const char *metric, int table_id)
484{
485 struct route_option *ro;
486 ALLOC_OBJ_GC(ro, struct route_option, l->gc);
487 ro->network = network;
488 ro->netmask = netmask;
489 ro->gateway = gateway;
490 ro->metric = metric;
491 ro->table_id = table_id;
492 ro->next = l->routes;
493 l->routes = ro;
494}
495
496void
498 const char *gateway, const char *metric, int table_id)
499{
500 struct route_ipv6_option *ro;
501 ALLOC_OBJ_GC(ro, struct route_ipv6_option, l->gc);
502 ro->prefix = prefix;
503 ro->gateway = gateway;
504 ro->metric = metric;
505 ro->table_id = table_id;
506 ro->next = l->routes_ipv6;
507 l->routes_ipv6 = ro;
508}
509
510static void
512{
513 gc_free(&rl->gc);
514 CLEAR(*rl);
515}
516
517static void
519{
520 gc_free(&rl6->gc);
521 CLEAR(*rl6);
522}
523
524void
525route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
526{
527 ASSERT(rl);
528 rl->spec.remote_endpoint = addr;
530 setenv_route_addr(es, "vpn_gateway", rl->spec.remote_endpoint, -1);
531}
532
533static void
535 in_addr_t target)
536{
537 if (rl->rgi.gateway.netmask < 0xFFFFFFFF)
538 {
539 struct route_ipv4 *r1, *r2;
540 unsigned int l2;
541
542 ALLOC_OBJ_GC(r1, struct route_ipv4, &rl->gc);
543 ALLOC_OBJ_GC(r2, struct route_ipv4, &rl->gc);
544
545 /* split a route into two smaller blocking routes, and direct them to target */
546 l2 = ((~gateway->netmask) + 1) >> 1;
547 r1->flags = RT_DEFINED;
548 r1->table_id = NET_ROUTE_TABLE_DEFAULT;
549 r1->gateway = target;
550 r1->network = gateway->addr & gateway->netmask;
551 r1->netmask = ~(l2 - 1);
552 r1->next = rl->routes;
553 rl->routes = r1;
554
555 *r2 = *r1;
556 r2->network += l2;
557 r2->next = rl->routes;
558 rl->routes = r2;
559 }
560}
561
562static void
564{
565#ifndef TARGET_ANDROID
566 /* add bypass for gateway addr */
568#endif
569
570 /* block access to local subnet */
572
573 /* process additional subnets on gateway interface */
574 for (int i = 0; i < rl->rgi.n_addrs; ++i)
575 {
576 const struct route_gateway_address *gwa = &rl->rgi.addrs[i];
577 /* omit the add/subnet in &rl->rgi which we processed above */
578 if (!((rl->rgi.gateway.addr & rl->rgi.gateway.netmask) == (gwa->addr & gwa->netmask)
579 && rl->rgi.gateway.netmask == gwa->netmask))
580 {
582 }
583 }
584}
585
586#if defined(__GNUC__) || defined(__clang__)
587#pragma GCC diagnostic push
588#pragma GCC diagnostic ignored "-Wsign-conversion"
589#endif
590
591bool
593{
594 const unsigned int rgi_needed = (RGI_ADDR_DEFINED | RGI_NETMASK_DEFINED);
595 return (rl->flags & RG_BLOCK_LOCAL) && (rl->rgi.flags & rgi_needed) == rgi_needed
597}
598
599bool
600init_route_list(struct route_list *rl, const struct route_option_list *opt,
601 const char *remote_endpoint, int default_metric, in_addr_t remote_host,
602 struct env_set *es, openvpn_net_ctx_t *ctx)
603{
604 struct gc_arena gc = gc_new();
605 bool ret = true;
606
608
609 rl->flags = opt->flags;
610
611 if (remote_host != IPV4_INVALID_ADDR)
612 {
613 rl->spec.remote_host = remote_host;
615 }
616
617 if (default_metric)
618 {
619 rl->spec.default_metric = default_metric;
621 }
622
623 get_default_gateway(&rl->ngi, INADDR_ANY, ctx);
624 if (rl->ngi.flags & RGI_ADDR_DEFINED)
625 {
626 setenv_route_addr(es, "net_gateway", rl->ngi.gateway.addr, -1);
627#ifndef ENABLE_SMALL
628 print_default_gateway(D_ROUTE, &rl->ngi, NULL);
629#endif
630 }
631 else
632 {
633 dmsg(D_ROUTE, "ROUTE: default_gateway=UNDEF");
634 }
635
636 get_default_gateway(&rl->rgi, remote_host != IPV4_INVALID_ADDR ? remote_host : INADDR_ANY, ctx);
637
638 if (rl->spec.flags & RTSA_REMOTE_HOST)
639 {
640 rl->spec.remote_host_local = test_local_addr(remote_host, &rl->rgi);
641 }
642
643 if (is_route_parm_defined(remote_endpoint))
644 {
645 bool defined = false;
648 0, &defined, NULL);
649
650 if (defined)
651 {
652 setenv_route_addr(es, "vpn_gateway", rl->spec.remote_endpoint, -1);
654 }
655 else
656 {
657 msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve default gateway: %s",
658 remote_endpoint);
659 ret = false;
660 }
661 }
662
663 if (rl->flags & RG_ENABLE)
664 {
665 if (block_local_needed(rl))
666 {
668 }
670#ifndef ENABLE_SMALL
672#endif
673 }
674
675 /* parse the routes from opt to rl */
676 {
677 const struct route_option *ro;
678 for (ro = opt->routes; ro; ro = ro->next)
679 {
680 struct addrinfo *netlist = NULL;
681 struct route_ipv4 r;
682
683 if (!init_route(&r, &netlist, ro, rl))
684 {
685 ret = false;
686 }
687 else
688 {
689 const struct addrinfo *curele;
690 for (curele = netlist; curele; curele = curele->ai_next)
691 {
692 struct route_ipv4 *new;
693 ALLOC_OBJ_GC(new, struct route_ipv4, &rl->gc);
694 *new = r;
695 new->network = ntohl(((struct sockaddr_in *)curele->ai_addr)->sin_addr.s_addr);
696 new->next = rl->routes;
697 rl->routes = new;
698 }
699 }
700 if (netlist)
701 {
703 }
704 }
705 }
706
707 gc_free(&gc);
708 return ret;
709}
710
711bool
712ipv6_net_contains_host(const struct in6_addr *network, unsigned int bits, const struct in6_addr *host)
713{
714 /* not the most beautiful implementation in the world, but portable and
715 * "good enough" */
716 if (bits > 128)
717 {
718 return false;
719 }
720
721 int i;
722 for (i = 0; bits >= 8; i++, bits -= 8)
723 {
724 if (network->s6_addr[i] != host->s6_addr[i])
725 {
726 return false;
727 }
728 }
729
730 if (bits == 0)
731 {
732 return true;
733 }
734
735 unsigned int mask = 0xff << (8 - bits);
736
737 if ((network->s6_addr[i] & mask) == (host->s6_addr[i] & mask))
738 {
739 return true;
740 }
741
742 return false;
743}
744
745bool
747 const char *remote_endpoint, int default_metric,
748 const struct in6_addr *remote_host_ipv6, struct env_set *es,
750{
751 struct gc_arena gc = gc_new();
752 bool ret = true;
753 bool need_remote_ipv6_route;
754
756
757 rl6->flags = opt6->flags;
758
759 if (remote_host_ipv6)
760 {
761 rl6->remote_host_ipv6 = *remote_host_ipv6;
763 }
764
765 if (default_metric >= 0)
766 {
767 rl6->default_metric = default_metric;
769 }
770
771 msg(D_ROUTE, "GDG6: remote_host_ipv6=%s",
772 remote_host_ipv6 ? print_in6_addr(*remote_host_ipv6, 0, &gc) : "n/a");
773
774 get_default_gateway_ipv6(&rl6->ngi6, NULL, ctx);
775 if (rl6->ngi6.flags & RGI_ADDR_DEFINED)
776 {
777 setenv_str(es, "net_gateway_ipv6", print_in6_addr(rl6->ngi6.gateway.addr_ipv6, 0, &gc));
778#ifndef ENABLE_SMALL
779 print_default_gateway(D_ROUTE, NULL, &rl6->ngi6);
780#endif
781 }
782 else
783 {
784 dmsg(D_ROUTE, "ROUTE6: default_gateway=UNDEF");
785 }
786
787 get_default_gateway_ipv6(&rl6->rgi6, remote_host_ipv6, ctx);
788
789 if (is_route_parm_defined(remote_endpoint))
790 {
791 if (inet_pton(AF_INET6, remote_endpoint, &rl6->remote_endpoint_ipv6) == 1)
792 {
794 }
795 else
796 {
797 msg(M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve VPN endpoint: %s",
798 remote_endpoint);
799 ret = false;
800 }
801 }
802
803 /* parse the routes from opt6 to rl6
804 * discovering potential overlaps with remote_host_ipv6 in the process
805 */
806 need_remote_ipv6_route = false;
807
808 {
809 const struct route_ipv6_option *ro6;
810 for (ro6 = opt6->routes_ipv6; ro6; ro6 = ro6->next)
811 {
812 struct route_ipv6 *r6;
813 ALLOC_OBJ_GC(r6, struct route_ipv6, &rl6->gc);
814 if (!init_route_ipv6(r6, ro6, rl6))
815 {
816 ret = false;
817 }
818 else
819 {
820 r6->next = rl6->routes_ipv6;
821 rl6->routes_ipv6 = r6;
822
823#ifndef TARGET_ANDROID
824 /* On Android the VPNService protect function call will take of
825 * avoiding routing loops, so ignore this part and let
826 * need_remote_ipv6_route always evaluate to false
827 */
828 if (remote_host_ipv6
829 && ipv6_net_contains_host(&r6->network, r6->netbits, remote_host_ipv6))
830 {
831 need_remote_ipv6_route = true;
832 msg(D_ROUTE,
833 "ROUTE6: %s/%u overlaps IPv6 remote %s, adding host route to VPN endpoint",
834 print_in6_addr(r6->network, 0, &gc), r6->netbits,
835 print_in6_addr(*remote_host_ipv6, 0, &gc));
836 }
837#endif
838 }
839 }
840 }
841
842 /* add VPN server host route if needed */
843 if (need_remote_ipv6_route)
844 {
847 {
848 struct route_ipv6 *r6;
849 ALLOC_OBJ_CLEAR_GC(r6, struct route_ipv6, &rl6->gc);
850
851 r6->network = *remote_host_ipv6;
852 r6->netbits = 128;
853 if (!(rl6->rgi6.flags & RGI_ON_LINK))
854 {
855 r6->gateway = rl6->rgi6.gateway.addr_ipv6;
856 }
857 r6->metric = 1;
858 r6->table_id = NET_ROUTE_TABLE_DEFAULT;
859#ifdef _WIN32
860 r6->adapter_index = rl6->rgi6.adapter_index;
861#else
862 r6->iface = rl6->rgi6.iface;
863#endif
865
866 r6->next = rl6->routes_ipv6;
867 rl6->routes_ipv6 = r6;
868 }
869 else
870 {
871 msg(M_WARN,
872 "ROUTE6: IPv6 route overlaps with IPv6 remote address, but could not determine IPv6 gateway address + interface, expect failure\n");
873 }
874 }
875
876 gc_free(&gc);
877 return ret;
878}
879
880static bool
882 unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es,
884{
885 struct route_ipv4 r;
886 CLEAR(r);
887 r.flags = RT_DEFINED;
889 r.network = network;
890 r.netmask = netmask;
891 r.gateway = gateway;
892 return add_route(&r, tt, flags, rgi, es, ctx);
893}
894
895static void
897 unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es,
899{
900 struct route_ipv4 r;
901 CLEAR(r);
904 r.network = network;
905 r.netmask = netmask;
906 r.gateway = gateway;
907 delete_route(&r, tt, flags, rgi, es, ctx);
908}
909
910static bool
912 const struct tuntap *tt, unsigned int flags,
913 const struct route_gateway_info *rgi,
914 const struct env_set *es, openvpn_net_ctx_t *ctx)
915{
916 int ret = true;
917 for (int i = 0; i < rb->n_bypass; ++i)
918 {
919 if (rb->bypass[i])
920 {
922 rgi, es, ctx)
923 && ret;
924 }
925 }
926 return ret;
927}
928
929static void
931 const struct tuntap *tt, unsigned int flags,
932 const struct route_gateway_info *rgi,
933 const struct env_set *es, openvpn_net_ctx_t *ctx)
934{
935 int i;
936 for (i = 0; i < rb->n_bypass; ++i)
937 {
938 if (rb->bypass[i])
939 {
941 ctx);
942 }
943 }
944}
945
946static bool
947redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt, unsigned int flags,
948 const struct env_set *es, openvpn_net_ctx_t *ctx)
949{
950 const char err[] = "NOTE: unable to redirect IPv4 default gateway --";
951 bool ret = true;
952
953 if (rl && rl->flags & RG_ENABLE)
954 {
955 bool local = rl->flags & RG_LOCAL;
956
957 if (!(rl->spec.flags & RTSA_REMOTE_ENDPOINT) && (rl->flags & RG_REROUTE_GW))
958 {
959 msg(M_WARN, "%s VPN gateway parameter (--route-gateway or --ifconfig) is missing", err);
960 ret = false;
961 }
962 /*
963 * check if a default route is defined, unless:
964 * - we are connecting to a remote host in our network
965 * - we are connecting to a non-IPv4 remote host (i.e. we use IPv6)
966 */
967 else if (!(rl->rgi.flags & RGI_ADDR_DEFINED) && !local
968 && (rl->spec.flags & RTSA_REMOTE_HOST))
969 {
970 msg(M_WARN, "%s Cannot read current default gateway from system", err);
971 ret = false;
972 }
973 else
974 {
975#ifndef TARGET_ANDROID
976 if (rl->flags & RG_AUTO_LOCAL)
977 {
978 const int tla = rl->spec.remote_host_local;
979 if (tla == TLA_NONLOCAL)
980 {
981 dmsg(D_ROUTE, "ROUTE remote_host is NOT LOCAL");
982 local = false;
983 }
984 else if (tla == TLA_LOCAL)
985 {
986 dmsg(D_ROUTE, "ROUTE remote_host is LOCAL");
987 local = true;
988 }
989 }
990 if (!local)
991 {
992 /* route remote host to original default gateway */
993 /* if remote_host is not ipv4 (ie: ipv6), just skip
994 * adding this special /32 route */
995 if ((rl->spec.flags & RTSA_REMOTE_HOST)
997 {
999 tt, flags | ROUTE_REF_GW, &rl->rgi, es, ctx);
1000 if (ret)
1001 {
1002 rl->iflags |= RL_DID_LOCAL;
1003 }
1004 }
1005 else
1006 {
1007 dmsg(D_ROUTE, "ROUTE remote_host protocol differs from tunneled");
1008 }
1009 }
1010#endif /* ifndef TARGET_ANDROID */
1011
1012 /* route DHCP/DNS server traffic through original default gateway */
1013 ret = add_bypass_routes(&rl->spec.bypass, rl->rgi.gateway.addr, tt, flags, &rl->rgi, es,
1014 ctx)
1015 && ret;
1016
1017 if (rl->flags & RG_REROUTE_GW)
1018 {
1019 if (rl->flags & RG_DEF1)
1020 {
1021 /* add new default route (1st component) */
1022 ret = add_route3(0x00000000, 0x80000000, rl->spec.remote_endpoint, tt, flags,
1023 &rl->rgi, es, ctx)
1024 && ret;
1025
1026 /* add new default route (2nd component) */
1027 ret = add_route3(0x80000000, 0x80000000, rl->spec.remote_endpoint, tt, flags,
1028 &rl->rgi, es, ctx)
1029 && ret;
1030 }
1031 else
1032 {
1033 /* don't try to remove the def route if it does not exist */
1034 if (rl->rgi.flags & RGI_ADDR_DEFINED)
1035 {
1036 /* delete default route */
1037 del_route3(0, 0, rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW, &rl->rgi,
1038 es, ctx);
1039 }
1040
1041 /* add new default route */
1042 ret = add_route3(0, 0, rl->spec.remote_endpoint, tt, flags, &rl->rgi, es, ctx)
1043 && ret;
1044 }
1045 }
1046
1047 /* set a flag so we can undo later */
1049 }
1050 }
1051 return ret;
1052}
1053
1054static void
1056 unsigned int flags, const struct env_set *es,
1057 openvpn_net_ctx_t *ctx)
1058{
1060 {
1061 /* delete remote host route */
1062 if (rl->iflags & RL_DID_LOCAL)
1063 {
1065 flags | ROUTE_REF_GW, &rl->rgi, es, ctx);
1066 rl->iflags &= ~RL_DID_LOCAL;
1067 }
1068
1069 /* delete special DHCP/DNS bypass route */
1070 del_bypass_routes(&rl->spec.bypass, rl->rgi.gateway.addr, tt, flags, &rl->rgi, es, ctx);
1071
1072 if (rl->flags & RG_REROUTE_GW)
1073 {
1074 if (rl->flags & RG_DEF1)
1075 {
1076 /* delete default route (1st component) */
1077 del_route3(0x00000000, 0x80000000, rl->spec.remote_endpoint, tt, flags, &rl->rgi,
1078 es, ctx);
1079
1080 /* delete default route (2nd component) */
1081 del_route3(0x80000000, 0x80000000, rl->spec.remote_endpoint, tt, flags, &rl->rgi,
1082 es, ctx);
1083 }
1084 else
1085 {
1086 /* delete default route */
1087 del_route3(0, 0, rl->spec.remote_endpoint, tt, flags, &rl->rgi, es, ctx);
1088 /* restore original default route if there was any */
1089 if (rl->rgi.flags & RGI_ADDR_DEFINED)
1090 {
1091 add_route3(0, 0, rl->rgi.gateway.addr, tt, flags | ROUTE_REF_GW, &rl->rgi, es,
1092 ctx);
1093 }
1094 }
1095 }
1096
1098 }
1099}
1100
1101bool
1102add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
1103 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
1104{
1105 bool ret = redirect_default_route_to_vpn(rl, tt, flags, es, ctx);
1106 if (rl && !(rl->iflags & RL_ROUTES_ADDED))
1107 {
1108 struct route_ipv4 *r;
1109
1110 if (rl->routes && !tt->did_ifconfig_setup)
1111 {
1112 msg(M_INFO,
1113 "WARNING: OpenVPN was configured to add an IPv4 "
1114 "route. However, no IPv4 has been configured for %s, "
1115 "therefore the route installation may fail or may not work "
1116 "as expected.",
1117 tt->actual_name);
1118 }
1119
1120#ifdef ENABLE_MANAGEMENT
1121 if (management && rl->routes)
1122 {
1124 NULL);
1125 }
1126#endif
1127
1128 for (r = rl->routes; r; r = r->next)
1129 {
1131 {
1132 delete_route(r, tt, flags, &rl->rgi, es, ctx);
1133 }
1134 ret = add_route(r, tt, flags, &rl->rgi, es, ctx) && ret;
1135 }
1136 rl->iflags |= RL_ROUTES_ADDED;
1137 }
1138 if (rl6 && !(rl6->iflags & RL_ROUTES_ADDED))
1139 {
1140 struct route_ipv6 *r;
1141
1142 if (!tt->did_ifconfig_ipv6_setup)
1143 {
1144 msg(M_INFO,
1145 "WARNING: OpenVPN was configured to add an IPv6 "
1146 "route. However, no IPv6 has been configured for %s, "
1147 "therefore the route installation may fail or may not work "
1148 "as expected.",
1149 tt->actual_name);
1150 }
1151
1152 for (r = rl6->routes_ipv6; r; r = r->next)
1153 {
1155 {
1156 delete_route_ipv6(r, tt, es, ctx);
1157 }
1158 ret = add_route_ipv6(r, tt, flags, es, ctx) && ret;
1159 }
1160 rl6->iflags |= RL_ROUTES_ADDED;
1161 }
1162
1163 return ret;
1164}
1165
1166void
1167delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt,
1168 unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
1169{
1170 delete_routes_v4(rl, tt, flags, es, ctx);
1171 delete_routes_v6(rl6, tt, flags, es, ctx);
1172}
1173
1174void
1175delete_routes_v4(struct route_list *rl, const struct tuntap *tt, unsigned int flags,
1176 const struct env_set *es, openvpn_net_ctx_t *ctx)
1177{
1178 if (rl && (rl->iflags & RL_ROUTES_ADDED))
1179 {
1180 struct route_ipv4 *r;
1181 for (r = rl->routes; r; r = r->next)
1182 {
1183 delete_route(r, tt, flags, &rl->rgi, es, ctx);
1184 }
1185 rl->iflags &= ~RL_ROUTES_ADDED;
1186 }
1187
1188 undo_redirect_default_route_to_vpn(rl, tt, flags, es, ctx);
1189
1190 if (rl)
1191 {
1192 clear_route_list(rl);
1193 }
1194}
1195
1196void
1197delete_routes_v6(struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags,
1198 const struct env_set *es, openvpn_net_ctx_t *ctx)
1199{
1200 if (rl6 && (rl6->iflags & RL_ROUTES_ADDED))
1201 {
1202 const struct route_ipv6 *r6;
1203 for (r6 = rl6->routes_ipv6; r6; r6 = r6->next)
1204 {
1205 delete_route_ipv6(r6, tt, es, ctx);
1206 }
1207 rl6->iflags &= ~RL_ROUTES_ADDED;
1208 }
1209
1210 if (rl6)
1211 {
1213 }
1214}
1215
1216#ifndef ENABLE_SMALL
1217
1218static const char *
1219show_opt(const char *option)
1220{
1221 if (!option)
1222 {
1223 return "default (not set)";
1224 }
1225 else
1226 {
1227 return option;
1228 }
1229}
1230
1231static void
1232print_route_option(const struct route_option *ro, msglvl_t msglevel)
1233{
1234 msg(msglevel, " route %s/%s/%s/%s", show_opt(ro->network), show_opt(ro->netmask),
1235 show_opt(ro->gateway), show_opt(ro->metric));
1236}
1237
1238void
1240{
1241 const struct route_option *ro;
1242 if (rol->flags & RG_ENABLE)
1243 {
1244 msg(msglevel, " [redirect_default_gateway local=%d]", (rol->flags & RG_LOCAL) != 0);
1245 }
1246 for (ro = rol->routes; ro; ro = ro->next)
1247 {
1248 print_route_option(ro, msglevel);
1249 }
1250}
1251
1252void
1253print_default_gateway(const msglvl_t msglevel, const struct route_gateway_info *rgi,
1254 const struct route_ipv6_gateway_info *rgi6)
1255{
1256 struct gc_arena gc = gc_new();
1257 if (rgi && (rgi->flags & RGI_ADDR_DEFINED))
1258 {
1259 struct buffer out = alloc_buf_gc(256, &gc);
1260 buf_printf(&out, "ROUTE_GATEWAY");
1261 if (rgi->flags & RGI_ON_LINK)
1262 {
1263 buf_printf(&out, " ON_LINK");
1264 }
1265 else
1266 {
1267 buf_printf(&out, " %s", print_in_addr_t(rgi->gateway.addr, 0, &gc));
1268 }
1269 if (rgi->flags & RGI_NETMASK_DEFINED)
1270 {
1271 buf_printf(&out, "/%s", print_in_addr_t(rgi->gateway.netmask, 0, &gc));
1272 }
1273#ifdef _WIN32
1274 if (rgi->flags & RGI_IFACE_DEFINED)
1275 {
1276 buf_printf(&out, " I=%lu", rgi->adapter_index);
1277 }
1278#else
1279 if (rgi->flags & RGI_IFACE_DEFINED)
1280 {
1281 buf_printf(&out, " IFACE=%s", rgi->iface);
1282 }
1283#endif
1284 if (rgi->flags & RGI_HWADDR_DEFINED)
1285 {
1286 buf_printf(&out, " HWADDR=%s", format_hex_ex(rgi->hwaddr, 6, 0, 1, ":", &gc));
1287 }
1288 msg(msglevel, "%s", BSTR(&out));
1289 }
1290
1291 if (rgi6 && (rgi6->flags & RGI_ADDR_DEFINED))
1292 {
1293 struct buffer out = alloc_buf_gc(256, &gc);
1294 buf_printf(&out, "ROUTE6_GATEWAY");
1295 buf_printf(&out, " %s", print_in6_addr(rgi6->gateway.addr_ipv6, 0, &gc));
1296 if (rgi6->flags & RGI_ON_LINK)
1297 {
1298 buf_printf(&out, " ON_LINK");
1299 }
1300 if (rgi6->flags & RGI_NETMASK_DEFINED)
1301 {
1302 buf_printf(&out, "/%d", rgi6->gateway.netbits_ipv6);
1303 }
1304#ifdef _WIN32
1305 if (rgi6->flags & RGI_IFACE_DEFINED)
1306 {
1307 buf_printf(&out, " I=%lu", rgi6->adapter_index);
1308 }
1309#else
1310 if (rgi6->flags & RGI_IFACE_DEFINED)
1311 {
1312 buf_printf(&out, " IFACE=%s", rgi6->iface);
1313 }
1314#endif
1315 if (rgi6->flags & RGI_HWADDR_DEFINED)
1316 {
1317 buf_printf(&out, " HWADDR=%s", format_hex_ex(rgi6->hwaddr, 6, 0, 1, ":", &gc));
1318 }
1319 msg(msglevel, "%s", BSTR(&out));
1320 }
1321 gc_free(&gc);
1322}
1323
1324#endif /* ifndef ENABLE_SMALL */
1325
1326static void
1327setenv_route(struct env_set *es, const struct route_ipv4 *r, int i)
1328{
1329 if (r->flags & RT_DEFINED)
1330 {
1331 setenv_route_addr(es, "network", r->network, i);
1332 setenv_route_addr(es, "netmask", r->netmask, i);
1333 setenv_route_addr(es, "gateway", r->gateway, i);
1334
1335 if (r->flags & RT_METRIC_DEFINED)
1336 {
1337 setenv_int_i(es, "route_metric", r->metric, i);
1338 }
1339 }
1340}
1341
1342void
1343setenv_routes(struct env_set *es, const struct route_list *rl)
1344{
1345 int i = 1;
1346 const struct route_ipv4 *r;
1347 for (r = rl->routes; r; r = r->next)
1348 {
1349 setenv_route(es, r, i++);
1350 }
1351}
1352
1353static void
1354setenv_route_ipv6(struct env_set *es, const struct route_ipv6 *r6, int i)
1355{
1356 struct gc_arena gc = gc_new();
1357 if (r6->flags & RT_DEFINED)
1358 {
1359 struct buffer val = alloc_buf_gc(256, &gc);
1360
1361 buf_printf(&val, "%s/%u", print_in6_addr(r6->network, 0, &gc), r6->netbits);
1362 setenv_str_i(es, "route_ipv6_network", BSTR(&val), i);
1363 setenv_str_i(es, "route_ipv6_gateway", print_in6_addr(r6->gateway, 0, &gc), i);
1364
1365 if (r6->flags & RT_METRIC_DEFINED)
1366 {
1367 setenv_int_i(es, "route_ipv6_metric", r6->metric, i);
1368 }
1369 }
1370 gc_free(&gc);
1371}
1372void
1373setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6)
1374{
1375 int i = 1;
1376 const struct route_ipv6 *r6;
1377 for (r6 = rl6->routes_ipv6; r6; r6 = r6->next)
1378 {
1379 setenv_route_ipv6(es, r6, i++);
1380 }
1381}
1382
1383/*
1384 * local_route() determines whether the gateway of a provided host
1385 * route is on the same interface that owns the default gateway.
1386 * It uses the data structure
1387 * returned by get_default_gateway() (struct route_gateway_info)
1388 * to determine this. If the route is local, LR_MATCH is returned.
1389 * When adding routes into the kernel, if LR_MATCH is defined for
1390 * a given route, the route should explicitly reference the default
1391 * gateway interface as the route destination. For example, here
1392 * is an example on Linux that uses LR_MATCH:
1393 *
1394 * route add -net 10.10.0.1 netmask 255.255.255.255 dev eth0
1395 *
1396 * This capability is needed by the "default-gateway block-local"
1397 * directive, to allow client access to the local subnet to be
1398 * blocked but still allow access to the local default gateway.
1399 */
1400
1401/* local_route() return values */
1402#define LR_NOMATCH 0 /* route is not local */
1403#define LR_MATCH 1 /* route is local */
1404
1405static int
1407 const struct route_gateway_info *rgi)
1408{
1409 /* set LR_MATCH on local host routes */
1410 const unsigned int rgi_needed = (RGI_ADDR_DEFINED | RGI_NETMASK_DEFINED | RGI_IFACE_DEFINED);
1411 if (rgi && (rgi->flags & rgi_needed) == rgi_needed && gateway == rgi->gateway.addr
1412 && netmask == 0xFFFFFFFF)
1413 {
1414 if (((network ^ rgi->gateway.addr) & rgi->gateway.netmask) == 0)
1415 {
1416 return LR_MATCH;
1417 }
1418 else
1419 {
1420 /* examine additional subnets on gateway interface */
1421 for (int i = 0; i < rgi->n_addrs; ++i)
1422 {
1423 const struct route_gateway_address *gwa = &rgi->addrs[i];
1424 if (((network ^ gwa->addr) & gwa->netmask) == 0)
1425 {
1426 return LR_MATCH;
1427 }
1428 }
1429 }
1430 }
1431 return LR_NOMATCH;
1432}
1433
1434/* Return true if the "on-link" form of the route should be used. This is when the gateway for
1435 * a route is specified as an interface rather than an address. */
1436#if defined(TARGET_LINUX) || defined(_WIN32) || defined(TARGET_DARWIN)
1437static inline bool
1438is_on_link(const struct route_ipv4 *r, const unsigned int flags,
1439 const struct route_gateway_info *rgi)
1440{
1441 return rgi
1442 && (local_route(r->network, r->netmask, r->gateway, rgi) == LR_MATCH
1443 || ((flags & ROUTE_REF_GW) && (rgi->flags & RGI_ON_LINK)));
1444}
1445#endif
1446
1447bool
1448add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
1449 const struct route_gateway_info *rgi, /* may be NULL */
1450 const struct env_set *es, openvpn_net_ctx_t *ctx)
1451{
1452 int status = 0;
1453
1454 if (!(r->flags & RT_DEFINED))
1455 {
1456 return true; /* no error */
1457 }
1458
1459 struct argv argv = argv_new();
1460 struct gc_arena gc = gc_new();
1461
1462#if !defined(TARGET_LINUX)
1463 const char *network = print_in_addr_t(r->network, 0, &gc);
1464#if !defined(TARGET_AIX)
1465 const char *netmask = print_in_addr_t(r->netmask, 0, &gc);
1466#endif
1467 const char *gateway = print_in_addr_t(r->gateway, 0, &gc);
1468#endif
1469
1470
1471#if defined(TARGET_LINUX)
1472 const char *iface = NULL;
1473 int metric = -1;
1474
1475 if (is_on_link(r, flags, rgi))
1476 {
1477 iface = rgi->iface;
1478 }
1479
1480 if (r->flags & RT_METRIC_DEFINED)
1481 {
1482 metric = r->metric;
1483 }
1484
1485
1487 int ret = net_route_v4_add(ctx, &r->network, netmask_to_netbits2(r->netmask), &r->gateway,
1488 iface, r->table_id, metric);
1489 if (ret == -EEXIST)
1490 {
1491 msg(D_ROUTE, "NOTE: Linux route add command failed because route exists");
1493 }
1494 else if (ret < 0)
1495 {
1496 msg(M_WARN, "ERROR: Linux route add command failed");
1497 status = RTA_ERROR;
1498 }
1499
1500#elif defined(TARGET_ANDROID)
1501 char out[128];
1502
1503 if (rgi)
1504 {
1505 snprintf(out, sizeof(out), "%s %s %s dev %s", network, netmask, gateway, rgi->iface);
1506 }
1507 else
1508 {
1509 snprintf(out, sizeof(out), "%s %s %s", network, netmask, gateway);
1510 }
1511 bool ret = management_android_control(management, "ROUTE", out);
1512 status = ret ? RTA_SUCCESS : RTA_ERROR;
1513
1514#elif defined(_WIN32)
1515 {
1516 DWORD ai = TUN_ADAPTER_INDEX_INVALID;
1517 argv_printf(&argv, "%s%s ADD %s MASK %s %s", get_win_sys_path(), WIN_ROUTE_PATH_SUFFIX,
1518 network, netmask, gateway);
1519 if (r->flags & RT_METRIC_DEFINED)
1520 {
1521 argv_printf_cat(&argv, "METRIC %d", r->metric);
1522 }
1523 if (is_on_link(r, flags, rgi))
1524 {
1525 ai = rgi->adapter_index;
1526 argv_printf_cat(&argv, "IF %lu", ai);
1527 }
1528
1530
1531 const char *method = "service";
1532 if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_SERVICE)
1533 {
1534 status = add_route_service(r, tt);
1535 }
1536 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_IPAPI)
1537 {
1538 status = add_route_ipapi(r, tt, ai);
1539 method = "ipapi";
1540 }
1541 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_EXE)
1542 {
1544 bool ret =
1545 openvpn_execve_check(&argv, es, 0, "ERROR: Windows route add command failed");
1546 status = ret ? RTA_SUCCESS : RTA_ERROR;
1548 method = "route.exe";
1549 }
1550 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_ADAPTIVE)
1551 {
1552 status = add_route_ipapi(r, tt, ai);
1553 method = "ipapi [adaptive]";
1554 if (status == RTA_ERROR)
1555 {
1556 msg(D_ROUTE, "Route addition fallback to route.exe");
1558 bool ret = openvpn_execve_check(
1559 &argv, es, 0, "ERROR: Windows route add command failed [adaptive]");
1560 status = ret ? RTA_SUCCESS : RTA_ERROR;
1562 method = "route.exe";
1563 }
1564 }
1565 else
1566 {
1567 ASSERT(0);
1568 }
1569 if (status != RTA_ERROR) /* error is logged upstream */
1570 {
1571 msg(D_ROUTE, "Route addition via %s %s", method,
1572 (status == RTA_SUCCESS) ? "succeeded" : "failed because route exists");
1573 }
1574 }
1575
1576#elif defined(TARGET_SOLARIS)
1577
1578 /* example: route add 192.0.2.32 -netmask 255.255.255.224 somegateway */
1579
1580 argv_printf(&argv, "%s add", ROUTE_PATH);
1581
1582 argv_printf_cat(&argv, "%s -netmask %s %s", network, netmask, gateway);
1583
1584 /* Solaris can only distinguish between "metric 0" == "on-link on the
1585 * interface where the IP address given is configured" and "metric > 0"
1586 * == "use gateway specified" (no finer-grained route metrics available)
1587 *
1588 * More recent versions of Solaris can also do "-interface", but that
1589 * would break backwards compatibility with older versions for no gain.
1590 */
1591 if (r->flags & RT_METRIC_DEFINED)
1592 {
1593 argv_printf_cat(&argv, "%d", r->metric);
1594 }
1595
1597 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route add command failed");
1598 status = ret ? RTA_SUCCESS : RTA_ERROR;
1599
1600#elif defined(TARGET_FREEBSD)
1601
1602 argv_printf(&argv, "%s add", ROUTE_PATH);
1603
1604#if 0
1605 if (r->flags & RT_METRIC_DEFINED)
1606 {
1607 argv_printf_cat(&argv, "-rtt %d", r->metric);
1608 }
1609#endif
1610
1611 argv_printf_cat(&argv, "-net %s %s %s", network, gateway, netmask);
1612
1613 /* FIXME -- add on-link support for FreeBSD */
1614
1616 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route add command failed");
1617 status = ret ? RTA_SUCCESS : RTA_ERROR;
1618
1619#elif defined(TARGET_DRAGONFLY)
1620
1621 argv_printf(&argv, "%s add", ROUTE_PATH);
1622
1623#if 0
1624 if (r->flags & RT_METRIC_DEFINED)
1625 {
1626 argv_printf_cat(&argv, "-rtt %d", r->metric);
1627 }
1628#endif
1629
1630 argv_printf_cat(&argv, "-net %s %s %s", network, gateway, netmask);
1631
1632 /* FIXME -- add on-link support for Dragonfly */
1633
1635 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: DragonFly route add command failed");
1636 status = ret ? RTA_SUCCESS : RTA_ERROR;
1637
1638#elif defined(TARGET_DARWIN)
1639
1640 argv_printf(&argv, "%s add", ROUTE_PATH);
1641
1642#if 0
1643 if (r->flags & RT_METRIC_DEFINED)
1644 {
1645 argv_printf_cat(&argv, "-rtt %d", r->metric);
1646 }
1647#endif
1648
1649 if (is_on_link(r, flags, rgi))
1650 {
1651 /* Mac OS X route syntax for ON_LINK:
1652 * route add -cloning -net 10.10.0.1 -netmask 255.255.255.255 -interface en0 */
1653 argv_printf_cat(&argv, "-cloning -net %s -netmask %s -interface %s", network, netmask,
1654 rgi->iface);
1655 }
1656 else
1657 {
1658 argv_printf_cat(&argv, "-net %s %s %s", network, gateway, netmask);
1659 }
1660
1662 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: OS X route add command failed");
1663 status = ret ? RTA_SUCCESS : RTA_ERROR;
1664
1665#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
1666
1667 argv_printf(&argv, "%s add", ROUTE_PATH);
1668
1669#if 0
1670 if (r->flags & RT_METRIC_DEFINED)
1671 {
1672 argv_printf_cat(&argv, "-rtt %d", r->metric);
1673 }
1674#endif
1675
1676 argv_printf_cat(&argv, "-net %s %s -netmask %s", network, gateway, netmask);
1677
1678 /* FIXME -- add on-link support for OpenBSD/NetBSD */
1679
1681 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD/NetBSD route add command failed");
1682 status = ret ? RTA_SUCCESS : RTA_ERROR;
1683
1684#elif defined(TARGET_AIX)
1685
1686 {
1687 int netbits = netmask_to_netbits2(r->netmask);
1688 argv_printf(&argv, "%s add -net %s/%d %s", ROUTE_PATH, network, netbits, gateway);
1690 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: AIX route add command failed");
1691 status = ret ? RTA_SUCCESS : RTA_ERROR;
1692 }
1693
1694#elif defined(TARGET_HAIKU)
1695
1696 /* ex: route add /dev/net/ipro1000/0 0.0.0.0 gw 192.168.1.1 netmask 128.0.0.0 */
1697 argv_printf(&argv, "%s add %s inet %s gw %s netmask %s", ROUTE_PATH, rgi->iface, network,
1698 gateway, netmask);
1700 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet route add command failed");
1701 status = ret ? RTA_SUCCESS : RTA_ERROR;
1702
1703#else /* if defined(TARGET_LINUX) */
1704 msg(M_FATAL,
1705 "Sorry, but I don't know how to do 'route' commands on this operating system. Try putting your routes in a --route-up script");
1706#endif /* if defined(TARGET_LINUX) */
1707
1708 if (status == RTA_SUCCESS)
1709 {
1710 r->flags |= RT_ADDED;
1711 }
1712 else
1713 {
1714 r->flags &= ~RT_ADDED;
1715 }
1716 argv_free(&argv);
1717 gc_free(&gc);
1718 /* release resources potentially allocated during route setup */
1719 net_ctx_reset(ctx);
1720
1721 return (status != RTA_ERROR);
1722}
1723
1724void
1726{
1727 /* clear host bit parts of route
1728 * (needed if routes are specified improperly, or if we need to
1729 * explicitly setup/clear the "connected" network routes on some OSes)
1730 */
1731 int byte = 15;
1732 int bits_to_clear = 128 - r6->netbits;
1733
1734 while (byte >= 0 && bits_to_clear > 0)
1735 {
1736 if (bits_to_clear >= 8)
1737 {
1738 r6->network.s6_addr[byte--] = 0;
1739 bits_to_clear -= 8;
1740 }
1741 else
1742 {
1743 r6->network.s6_addr[byte--] &= (uint8_t)(0xff << bits_to_clear);
1744 bits_to_clear = 0;
1745 }
1746 }
1747}
1748
1749bool
1750add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flags,
1751 const struct env_set *es, openvpn_net_ctx_t *ctx)
1752{
1753 int status = 0;
1754 bool gateway_needed = false;
1755
1756 if (!(r6->flags & RT_DEFINED))
1757 {
1758 return true; /* no error */
1759 }
1760
1761 struct argv argv = argv_new();
1762 struct gc_arena gc = gc_new();
1763
1764#ifndef _WIN32
1765 const char *device = tt->actual_name;
1766 if (r6->iface != NULL) /* vpn server special route */
1767 {
1768 device = r6->iface;
1769 if (!IN6_IS_ADDR_UNSPECIFIED(&r6->gateway))
1770 {
1771 gateway_needed = true;
1772 }
1773 }
1774#endif
1775
1777 const char *network = print_in6_addr(r6->network, 0, &gc);
1778 const char *gateway = print_in6_addr(r6->gateway, 0, &gc);
1779
1780#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
1781 || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
1782
1783 /* the BSD platforms cannot specify gateway and interface independently,
1784 * but for link-local destinations, we MUST specify the interface, so
1785 * we build a combined "$gateway%$interface" gateway string
1786 */
1787 if (r6->iface != NULL && gateway_needed
1788 && IN6_IS_ADDR_LINKLOCAL(&r6->gateway)) /* fe80::...%intf */
1789 {
1790 size_t len = strlen(gateway) + 1 + strlen(r6->iface) + 1;
1791 char *tmp = gc_malloc(len, true, &gc);
1792 snprintf(tmp, len, "%s%%%s", gateway, r6->iface);
1793 gateway = tmp;
1794 }
1795#endif
1796
1797#ifndef _WIN32
1798 msg(D_ROUTE, "add_route_ipv6(%s/%u -> %s metric %d) dev %s", network, r6->netbits, gateway,
1799 r6->metric, device);
1800#else
1801 msg(D_ROUTE, "add_route_ipv6(%s/%u -> %s metric %d) IF %lu", network, r6->netbits, gateway,
1802 r6->metric, r6->adapter_index ? r6->adapter_index : tt->adapter_index);
1803#endif
1804
1805 /*
1806 * Filter out routes which are essentially no-ops
1807 * (not currently done for IPv6)
1808 */
1809
1810 /* On "tun" interface, we never set a gateway if the operating system
1811 * can do "route to interface" - it does not add value, as the target
1812 * dev already fully qualifies the route destination on point-to-point
1813 * interfaces. OTOH, on "tap" interface, we must always set the
1814 * gateway unless the route is to be an on-link network
1815 */
1816 if (tt->type == DEV_TYPE_TAP && !((r6->flags & RT_METRIC_DEFINED) && r6->metric == 0))
1817 {
1818 gateway_needed = true;
1819 }
1820
1821 if (gateway_needed && IN6_IS_ADDR_UNSPECIFIED(&r6->gateway))
1822 {
1823 msg(M_WARN,
1824 "ROUTE6 WARNING: " PACKAGE_NAME " needs a gateway "
1825 "parameter for a --route-ipv6 option and no default was set via "
1826 "--ifconfig-ipv6 or --route-ipv6-gateway option. Not installing "
1827 "IPv6 route to %s/%u.",
1828 network, r6->netbits);
1829 status = 0;
1830 goto done;
1831 }
1832
1833#if defined(TARGET_LINUX)
1834 int metric = -1;
1835 if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0))
1836 {
1837 metric = r6->metric;
1838 }
1839
1841 int ret = net_route_v6_add(ctx, &r6->network, r6->netbits, gateway_needed ? &r6->gateway : NULL,
1842 device, r6->table_id, metric);
1843 if (ret == -EEXIST)
1844 {
1845 msg(D_ROUTE, "NOTE: Linux route add command failed because route exists");
1847 }
1848 else if (ret < 0)
1849 {
1850 msg(M_WARN, "ERROR: Linux route add command failed");
1851 status = RTA_ERROR;
1852 }
1853
1854#elif defined(TARGET_ANDROID)
1855 char out[64];
1856
1857 snprintf(out, sizeof(out), "%s/%u %s", network, r6->netbits, device);
1858
1859 status = management_android_control(management, "ROUTE6", out);
1860
1861#elif defined(_WIN32)
1862
1863 if (tt->options.msg_channel)
1864 {
1866 }
1867 else
1868 {
1869 status = route_ipv6_ipapi(true, r6, tt);
1870 }
1871#elif defined(TARGET_SOLARIS)
1872
1873 /* example: route add -inet6 2001:db8::/32 somegateway 0 */
1874
1875 /* for some reason, routes to tun/tap do not work for me unless I set
1876 * "metric 0" - otherwise, the routes will be nicely installed, but
1877 * packets will just disappear somewhere. So we always use "0" now,
1878 * unless the route points to "gateway on other interface"...
1879 *
1880 * (Note: OpenSolaris can not specify host%interface gateways, so we just
1881 * use the GW addresses - it seems to still work for fe80:: addresses,
1882 * however this is done internally. NUD maybe?)
1883 */
1884 argv_printf(&argv, "%s add -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
1885
1886 /* on tun (not tap), not "elsewhere"? -> metric 0 */
1887 if (tt->type == DEV_TYPE_TUN && !r6->iface)
1888 {
1889 argv_printf_cat(&argv, "0");
1890 }
1891
1893 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route add -inet6 command failed");
1894 status = ret ? RTA_SUCCESS : RTA_ERROR;
1895
1896#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
1897
1898 argv_printf(&argv, "%s add -inet6 %s/%d", ROUTE_PATH, network, r6->netbits);
1899
1900 if (gateway_needed)
1901 {
1902 argv_printf_cat(&argv, "%s", gateway);
1903 }
1904 else
1905 {
1906 argv_printf_cat(&argv, "-iface %s", device);
1907 }
1908
1910 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: *BSD route add -inet6 command failed");
1911 status = ret ? RTA_SUCCESS : RTA_ERROR;
1912
1913#elif defined(TARGET_DARWIN)
1914
1915 argv_printf(&argv, "%s add -inet6 %s -prefixlen %d", ROUTE_PATH, network, r6->netbits);
1916
1917 if (gateway_needed)
1918 {
1919 argv_printf_cat(&argv, "%s", gateway);
1920 }
1921 else
1922 {
1923 argv_printf_cat(&argv, "-iface %s", device);
1924 }
1925
1927 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: MacOS X route add -inet6 command failed");
1928 status = ret ? RTA_SUCCESS : RTA_ERROR;
1929
1930#elif defined(TARGET_OPENBSD)
1931
1932 argv_printf(&argv, "%s add -inet6 %s -prefixlen %d %s", ROUTE_PATH, network, r6->netbits,
1933 gateway);
1934
1936 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD route add -inet6 command failed");
1937 status = ret ? RTA_SUCCESS : RTA_ERROR;
1938
1939#elif defined(TARGET_NETBSD)
1940
1941 argv_printf(&argv, "%s add -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
1942
1944 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: NetBSD route add -inet6 command failed");
1945 status = ret ? RTA_SUCCESS : RTA_ERROR;
1946
1947#elif defined(TARGET_AIX)
1948
1949 argv_printf(&argv, "%s add -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
1951 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: AIX route add command failed");
1952 status = ret ? RTA_SUCCESS : RTA_ERROR;
1953
1954#elif defined(TARGET_HAIKU)
1955
1956 /* ex: route add /dev/net/ipro1000/0 inet6 :: gw beef::cafe prefixlen 64 */
1957 argv_printf(&argv, "%s add %s inet6 %s gw %s prefixlen %d", ROUTE_PATH, r6->iface, network,
1958 gateway, r6->netbits);
1960 bool ret = openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet6 route add command failed");
1961 status = ret ? RTA_SUCCESS : RTA_ERROR;
1962
1963#else /* if defined(TARGET_LINUX) */
1964 msg(M_FATAL,
1965 "Sorry, but I don't know how to do 'route ipv6' commands on this operating system. Try putting your routes in a --route-up script");
1966#endif /* if defined(TARGET_LINUX) */
1967
1968done:
1969 if (status == RTA_SUCCESS)
1970 {
1971 r6->flags |= RT_ADDED;
1972 }
1973 else
1974 {
1975 r6->flags &= ~RT_ADDED;
1976 }
1977 argv_free(&argv);
1978 gc_free(&gc);
1979 /* release resources potentially allocated during route setup */
1980 net_ctx_reset(ctx);
1981
1982 return (status != RTA_ERROR);
1983}
1984
1985static void
1986delete_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags,
1987 const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
1988{
1989#if !defined(TARGET_LINUX)
1990 const char *network;
1991#if !defined(TARGET_AIX)
1992 const char *netmask;
1993#endif
1994 const char *gateway;
1995#else /* if !defined(TARGET_LINUX) */
1996 int metric;
1997#endif
1998
1999 if ((r->flags & (RT_DEFINED | RT_ADDED)) != (RT_DEFINED | RT_ADDED))
2000 {
2001 return;
2002 }
2003
2004 struct gc_arena gc = gc_new();
2005 struct argv argv = argv_new();
2006
2007#if !defined(TARGET_LINUX)
2008 network = print_in_addr_t(r->network, 0, &gc);
2009#if !defined(TARGET_AIX)
2010 netmask = print_in_addr_t(r->netmask, 0, &gc);
2011#endif
2012 gateway = print_in_addr_t(r->gateway, 0, &gc);
2013#endif
2014
2015
2016#if defined(TARGET_LINUX)
2017 metric = -1;
2018 if (r->flags & RT_METRIC_DEFINED)
2019 {
2020 metric = r->metric;
2021 }
2022
2023 if (net_route_v4_del(ctx, &r->network, netmask_to_netbits2(r->netmask), &r->gateway, NULL,
2024 r->table_id, metric)
2025 < 0)
2026 {
2027 msg(M_WARN, "ERROR: Linux route delete command failed");
2028 }
2029#elif defined(_WIN32)
2030
2031 argv_printf(&argv, "%s%s DELETE %s MASK %s %s", get_win_sys_path(), WIN_ROUTE_PATH_SUFFIX,
2032 network, netmask, gateway);
2033
2035
2036 if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_SERVICE)
2037 {
2038 const bool status = del_route_service(r, tt);
2039 msg(D_ROUTE, "Route deletion via service %s", status ? "succeeded" : "failed");
2040 }
2041 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_IPAPI)
2042 {
2043 const bool status = del_route_ipapi(r, tt);
2044 msg(D_ROUTE, "Route deletion via IPAPI %s", status ? "succeeded" : "failed");
2045 }
2046 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_EXE)
2047 {
2049 openvpn_execve_check(&argv, es, 0, "ERROR: Windows route delete command failed");
2051 }
2052 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_ADAPTIVE)
2053 {
2054 const bool status = del_route_ipapi(r, tt);
2055 msg(D_ROUTE, "Route deletion via IPAPI %s [adaptive]", status ? "succeeded" : "failed");
2056 if (!status)
2057 {
2058 msg(D_ROUTE, "Route deletion fallback to route.exe");
2060 openvpn_execve_check(&argv, es, 0,
2061 "ERROR: Windows route delete command failed [adaptive]");
2063 }
2064 }
2065 else
2066 {
2067 ASSERT(0);
2068 }
2069
2070#elif defined(TARGET_SOLARIS)
2071
2072 argv_printf(&argv, "%s delete %s -netmask %s %s", ROUTE_PATH, network, netmask, gateway);
2073
2075 openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route delete command failed");
2076
2077#elif defined(TARGET_FREEBSD)
2078
2079 argv_printf(&argv, "%s delete -net %s %s %s", ROUTE_PATH, network, gateway, netmask);
2080
2082 openvpn_execve_check(&argv, es, 0, "ERROR: FreeBSD route delete command failed");
2083
2084#elif defined(TARGET_DRAGONFLY)
2085
2086 argv_printf(&argv, "%s delete -net %s %s %s", ROUTE_PATH, network, gateway, netmask);
2087
2089 openvpn_execve_check(&argv, es, 0, "ERROR: DragonFly route delete command failed");
2090
2091#elif defined(TARGET_DARWIN)
2092
2093 if (is_on_link(r, flags, rgi))
2094 {
2095 argv_printf(&argv, "%s delete -cloning -net %s -netmask %s -interface %s", ROUTE_PATH,
2096 network, netmask, rgi->iface);
2097 }
2098 else
2099 {
2100 argv_printf(&argv, "%s delete -net %s %s %s", ROUTE_PATH, network, gateway, netmask);
2101 }
2102
2104 openvpn_execve_check(&argv, es, 0, "ERROR: OS X route delete command failed");
2105
2106#elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
2107
2108 argv_printf(&argv, "%s delete -net %s %s -netmask %s", ROUTE_PATH, network, gateway, netmask);
2109
2111 openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD/NetBSD route delete command failed");
2112
2113#elif defined(TARGET_ANDROID)
2114 /* Avoids the unused variables warnings that all other platforms use
2115 * by adding them to the error message. */
2116 msg(D_ROUTE_DEBUG, "Deleting routes on Android is not possible/not "
2117 "needed. The VpnService API allows routes to be set "
2118 "on connect only and will clean up automatically. "
2119 "Tried to delete route %s netmask %s gateway %s",
2120 network, netmask, gateway);
2121#elif defined(TARGET_AIX)
2122
2123 {
2124 int netbits = netmask_to_netbits2(r->netmask);
2125 argv_printf(&argv, "%s delete -net %s/%d %s", ROUTE_PATH, network, netbits, gateway);
2127 openvpn_execve_check(&argv, es, 0, "ERROR: AIX route delete command failed");
2128 }
2129
2130#elif defined(TARGET_HAIKU)
2131
2132 /* ex: route delete /dev/net/ipro1000/0 inet 192.168.0.0 gw 192.168.1.1 netmask 255.255.0.0 */
2133 argv_printf(&argv, "%s delete %s inet %s gw %s netmask %s", ROUTE_PATH, rgi->iface, network,
2134 gateway, netmask);
2136 openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet route delete command failed");
2137
2138#else /* if defined(TARGET_LINUX) */
2139 msg(M_FATAL,
2140 "Sorry, but I don't know how to do 'route' commands on this operating system. Try putting your routes in a --route-up script");
2141#endif /* if defined(TARGET_LINUX) */
2142
2143 r->flags &= ~RT_ADDED;
2144 argv_free(&argv);
2145 gc_free(&gc);
2146 /* release resources potentially allocated during route cleanup */
2147 net_ctx_reset(ctx);
2148}
2149
2150void
2151delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt, const struct env_set *es,
2152 openvpn_net_ctx_t *ctx)
2153{
2154 const char *network;
2155
2156 if ((r6->flags & (RT_DEFINED | RT_ADDED)) != (RT_DEFINED | RT_ADDED))
2157 {
2158 return;
2159 }
2160
2161#if !defined(_WIN32)
2162#if !defined(TARGET_LINUX)
2163 const char *gateway;
2164#endif
2165#if !defined(TARGET_SOLARIS)
2166 bool gateway_needed = false;
2167 const char *device = tt->actual_name;
2168 if (r6->iface != NULL) /* vpn server special route */
2169 {
2170 device = r6->iface;
2171 gateway_needed = true;
2172 }
2173 (void)device; /* unused on some platforms */
2174
2175 /* if we used a gateway on "add route", we also need to specify it on
2176 * delete, otherwise some OSes will refuse to delete the route
2177 */
2178 if (tt->type == DEV_TYPE_TAP && !((r6->flags & RT_METRIC_DEFINED) && r6->metric == 0))
2179 {
2180 gateway_needed = true;
2181 }
2182#endif
2183#endif
2184
2185 struct gc_arena gc = gc_new();
2186 struct argv argv = argv_new();
2187
2188 network = print_in6_addr(r6->network, 0, &gc);
2189#if !defined(TARGET_LINUX) && !defined(_WIN32)
2190 gateway = print_in6_addr(r6->gateway, 0, &gc);
2191#endif
2192
2193#if defined(TARGET_DARWIN) || defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY) \
2194 || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
2195
2196 /* the BSD platforms cannot specify gateway and interface independently,
2197 * but for link-local destinations, we MUST specify the interface, so
2198 * we build a combined "$gateway%$interface" gateway string
2199 */
2200 if (r6->iface != NULL && gateway_needed
2201 && IN6_IS_ADDR_LINKLOCAL(&r6->gateway)) /* fe80::...%intf */
2202 {
2203 size_t len = strlen(gateway) + 1 + strlen(r6->iface) + 1;
2204 char *tmp = gc_malloc(len, true, &gc);
2205 snprintf(tmp, len, "%s%%%s", gateway, r6->iface);
2206 gateway = tmp;
2207 }
2208#endif
2209
2210 msg(D_ROUTE, "delete_route_ipv6(%s/%u)", network, r6->netbits);
2211
2212#if defined(TARGET_LINUX)
2213 int metric = -1;
2214 if ((r6->flags & RT_METRIC_DEFINED) && (r6->metric > 0))
2215 {
2216 metric = r6->metric;
2217 }
2218
2219 if (net_route_v6_del(ctx, &r6->network, r6->netbits, gateway_needed ? &r6->gateway : NULL,
2220 device, r6->table_id, metric)
2221 < 0)
2222 {
2223 msg(M_WARN, "ERROR: Linux route v6 delete command failed");
2224 }
2225
2226#elif defined(_WIN32)
2227
2228 if (tt->options.msg_channel)
2229 {
2231 }
2232 else
2233 {
2234 route_ipv6_ipapi(false, r6, tt);
2235 }
2236#elif defined(TARGET_SOLARIS)
2237
2238 /* example: route delete -inet6 2001:db8::/32 somegateway */
2239
2240 argv_printf(&argv, "%s delete -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
2241
2243 openvpn_execve_check(&argv, es, 0, "ERROR: Solaris route delete -inet6 command failed");
2244
2245#elif defined(TARGET_FREEBSD) || defined(TARGET_DRAGONFLY)
2246
2247 argv_printf(&argv, "%s delete -inet6 %s/%d", ROUTE_PATH, network, r6->netbits);
2248
2249 if (gateway_needed)
2250 {
2251 argv_printf_cat(&argv, "%s", gateway);
2252 }
2253 else
2254 {
2255 argv_printf_cat(&argv, "-iface %s", device);
2256 }
2257
2259 openvpn_execve_check(&argv, es, 0, "ERROR: *BSD route delete -inet6 command failed");
2260
2261#elif defined(TARGET_DARWIN)
2262
2263 argv_printf(&argv, "%s delete -inet6 %s -prefixlen %d", ROUTE_PATH, network, r6->netbits);
2264
2265 if (gateway_needed)
2266 {
2267 argv_printf_cat(&argv, "%s", gateway);
2268 }
2269 else
2270 {
2271 argv_printf_cat(&argv, "-iface %s", device);
2272 }
2273
2275 openvpn_execve_check(&argv, es, 0, "ERROR: MacOS X route delete -inet6 command failed");
2276
2277#elif defined(TARGET_OPENBSD)
2278
2279 argv_printf(&argv, "%s delete -inet6 %s -prefixlen %d %s", ROUTE_PATH, network, r6->netbits,
2280 gateway);
2281
2283 openvpn_execve_check(&argv, es, 0, "ERROR: OpenBSD route delete -inet6 command failed");
2284
2285#elif defined(TARGET_NETBSD)
2286
2287 argv_printf(&argv, "%s delete -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
2288
2290 openvpn_execve_check(&argv, es, 0, "ERROR: NetBSD route delete -inet6 command failed");
2291
2292#elif defined(TARGET_AIX)
2293
2294 argv_printf(&argv, "%s delete -inet6 %s/%d %s", ROUTE_PATH, network, r6->netbits, gateway);
2296 openvpn_execve_check(&argv, es, 0, "ERROR: AIX route delete command failed");
2297
2298#elif defined(TARGET_ANDROID)
2299 msg(D_ROUTE_DEBUG, "Deleting routes on Android is not possible/not "
2300 "needed. The VpnService API allows routes to be set "
2301 "on connect only and will clean up automatically. "
2302 "Tried to delete %s gateway %s",
2303 network,
2304 gateway_needed ? gateway : "(not needed)");
2305#elif defined(TARGET_HAIKU)
2306
2307 /* ex: route delete /dev/net/ipro1000/0 inet6 :: gw beef::cafe prefixlen 64 */
2308 argv_printf(&argv, "%s delete %s inet6 %s gw %s prefixlen %d", ROUTE_PATH, r6->iface, network,
2309 gateway, r6->netbits);
2311 openvpn_execve_check(&argv, es, 0, "ERROR: Haiku inet6 route delete command failed");
2312
2313#else /* if defined(TARGET_LINUX) */
2314 msg(M_FATAL,
2315 "Sorry, but I don't know how to do 'route ipv6' commands on this operating system. Try putting your routes in a --route-down script");
2316#endif /* if defined(TARGET_LINUX) */
2317
2318 argv_free(&argv);
2319 gc_free(&gc);
2320 /* release resources potentially allocated during route cleanup */
2321 net_ctx_reset(ctx);
2322}
2323
2324/*
2325 * The --redirect-gateway option requires OS-specific code below
2326 * to get the current default gateway.
2327 */
2328
2329#if defined(_WIN32)
2330
2331static const MIB_IPFORWARDTABLE *
2333{
2334 ULONG size = 0;
2335 PMIB_IPFORWARDTABLE rt = NULL;
2336 DWORD status;
2337
2338 status = GetIpForwardTable(NULL, &size, TRUE);
2339 if (status == ERROR_INSUFFICIENT_BUFFER)
2340 {
2341 rt = (PMIB_IPFORWARDTABLE)gc_malloc(size, false, gc);
2342 status = GetIpForwardTable(rt, &size, TRUE);
2343 if (status != NO_ERROR)
2344 {
2345 msg(D_ROUTE, "NOTE: GetIpForwardTable returned error: %s (code=%lu)",
2347 rt = NULL;
2348 }
2349 }
2350 return rt;
2351}
2352
2353static int
2354test_route(const IP_ADAPTER_INFO *adapters, const in_addr_t gateway, DWORD *index)
2355{
2356 int count = 0;
2357 DWORD i = adapter_index_of_ip(adapters, gateway, &count, NULL);
2358 if (index)
2359 {
2360 *index = i;
2361 }
2362 return count;
2363}
2364
2365static void
2366test_route_helper(bool *ret, int *count, int *good, int *ambig, const IP_ADAPTER_INFO *adapters,
2367 const in_addr_t gateway)
2368{
2369 int c;
2370
2371 ++*count;
2372 c = test_route(adapters, gateway, NULL);
2373 if (c == 0)
2374 {
2375 *ret = false;
2376 }
2377 else
2378 {
2379 ++*good;
2380 }
2381 if (c > 1)
2382 {
2383 ++*ambig;
2384 }
2385}
2386
2387/*
2388 * If we tried to add routes now, would we succeed?
2389 */
2390bool
2391test_routes(const struct route_list *rl, const struct tuntap *tt)
2392{
2393 struct gc_arena gc = gc_new();
2394 const IP_ADAPTER_INFO *adapters = get_adapter_info_list(&gc);
2395 bool ret = false;
2396 int count = 0;
2397 int good = 0;
2398 int ambig = 0;
2399 int len = -1;
2400 bool adapter_up = false;
2401
2402 if (is_adapter_up(tt, adapters))
2403 {
2404 ret = true;
2405 adapter_up = true;
2406
2407 /* we do this test only if we have IPv4 routes to install, and if
2408 * the tun/tap interface has seen IPv4 ifconfig - because if we
2409 * have no IPv4, the check will always fail, failing tun init
2410 */
2411 if (rl && tt->did_ifconfig_setup)
2412 {
2413 const struct route_ipv4 *r;
2414 for (r = rl->routes, len = 0; r; r = r->next, ++len)
2415 {
2416 test_route_helper(&ret, &count, &good, &ambig, adapters, r->gateway);
2417 }
2418
2419 if ((rl->flags & RG_ENABLE) && (rl->spec.flags & RTSA_REMOTE_ENDPOINT))
2420 {
2421 test_route_helper(&ret, &count, &good, &ambig, adapters, rl->spec.remote_endpoint);
2422 }
2423 }
2424 }
2425
2426 msg(D_ROUTE, "TEST ROUTES: %d/%d succeeded len=%d ret=%u a=%d u/d=%s",
2427 good, count, len, ret, ambig, adapter_up ? "up" : "down");
2428
2429 gc_free(&gc);
2430 return ret;
2431}
2432
2433static const MIB_IPFORWARDROW *
2434get_default_gateway_row(const MIB_IPFORWARDTABLE *routes)
2435{
2436 struct gc_arena gc = gc_new();
2437 DWORD lowest_metric = MAXDWORD;
2438 const MIB_IPFORWARDROW *ret = NULL;
2439 int best = -1;
2440
2441 if (routes)
2442 {
2443 for (DWORD i = 0; i < routes->dwNumEntries; ++i)
2444 {
2445 const MIB_IPFORWARDROW *row = &routes->table[i];
2446 const in_addr_t net = ntohl(row->dwForwardDest);
2447 const in_addr_t mask = ntohl(row->dwForwardMask);
2448 const DWORD index = row->dwForwardIfIndex;
2449 const DWORD metric = row->dwForwardMetric1;
2450
2451 dmsg(D_ROUTE_DEBUG, "GDGR: route[%lu] %s/%s i=%lu m=%lu", i,
2452 print_in_addr_t((in_addr_t)net, 0, &gc), print_in_addr_t((in_addr_t)mask, 0, &gc),
2453 index, metric);
2454
2455 if (!net && !mask && metric < lowest_metric)
2456 {
2457 ret = row;
2458 lowest_metric = metric;
2459 best = i;
2460 }
2461 }
2462 }
2463
2464 dmsg(D_ROUTE_DEBUG, "GDGR: best=%d lm=%lu", best, lowest_metric);
2465
2466 gc_free(&gc);
2467 return ret;
2468}
2469
2481static DWORD
2482get_best_route(struct gc_arena *gc, SOCKADDR_INET *dest, MIB_IPFORWARD_ROW2 *best_route)
2483{
2484 DWORD best_if_index;
2485 DWORD status;
2486
2487 CLEAR(*best_route);
2488
2489 /* get the best interface index to reach dest */
2490 status = GetBestInterfaceEx((struct sockaddr *)dest, &best_if_index);
2491 if (status != NO_ERROR)
2492 {
2493 msg(D_ROUTE, "NOTE: GetBestInterfaceEx returned error: %s (code=%lu)",
2495 goto done;
2496 }
2497
2498 msg(D_ROUTE_DEBUG, "GetBestInterfaceEx() returned if=%lu", best_if_index);
2499
2500 /* get the routing information (such as NextHop) for the destination and interface */
2501 NET_LUID luid;
2502 CLEAR(luid);
2503 SOCKADDR_INET best_src;
2504 CLEAR(best_src);
2505 status = GetBestRoute2(&luid, best_if_index, NULL, dest, 0, best_route, &best_src);
2506 if (status != NO_ERROR)
2507 {
2508 msg(D_ROUTE, "NOTE: GetIpForwardEntry2 returned error: %s (code=%lu)",
2510 goto done;
2511 }
2512
2513done:
2514 return status;
2515}
2516
2517void
2519{
2520 CLEAR(*rgi);
2521
2522 struct gc_arena gc = gc_new();
2523
2524 /* convert in_addr_t into SOCKADDR_INET */
2525 SOCKADDR_INET sa;
2526 CLEAR(sa);
2527 sa.si_family = AF_INET;
2528 sa.Ipv4.sin_addr.s_addr = htonl(dest);
2529
2530 /* get the best route to the destination */
2531 MIB_IPFORWARD_ROW2 best_route;
2532 CLEAR(best_route);
2533 DWORD status = get_best_route(&gc, &sa, &best_route);
2534 if (status != NO_ERROR)
2535 {
2536 goto done;
2537 }
2538
2540 rgi->gateway.addr = ntohl(best_route.NextHop.Ipv4.sin_addr.S_un.S_addr);
2541 rgi->adapter_index = best_route.InterfaceIndex;
2542
2543 if (rgi->gateway.addr == INADDR_ANY)
2544 {
2545 rgi->flags |= RGI_ON_LINK;
2546 }
2547
2548 /* get netmask and MAC address */
2549 const IP_ADAPTER_INFO *adapters = get_adapter_info_list(&gc);
2550 const IP_ADAPTER_INFO *ai = get_adapter(adapters, rgi->adapter_index);
2551 if (ai)
2552 {
2553 memcpy(rgi->hwaddr, ai->Address, 6);
2554 rgi->flags |= RGI_HWADDR_DEFINED;
2555
2556 /* get netmask for non-onlink routes */
2557 in_addr_t nm = inet_addr(ai->IpAddressList.IpMask.String);
2558 if (!(rgi->flags & RGI_ON_LINK) && (nm != INADDR_NONE))
2559 {
2560 rgi->gateway.netmask = ntohl(nm);
2561 rgi->flags |= RGI_NETMASK_DEFINED;
2562 }
2563 }
2564
2565done:
2566 gc_free(&gc);
2567}
2568
2569static DWORD
2570windows_route_find_if_index(const struct route_ipv4 *r, const struct tuntap *tt)
2571{
2572 struct gc_arena gc = gc_new();
2573 DWORD ret = TUN_ADAPTER_INDEX_INVALID;
2574 int count = 0;
2575 const IP_ADAPTER_INFO *adapters = get_adapter_info_list(&gc);
2576 const IP_ADAPTER_INFO *tun_adapter = get_tun_adapter(tt, adapters);
2577 bool on_tun = false;
2578
2579 /* first test on tun interface */
2580 if (is_ip_in_adapter_subnet(tun_adapter, r->gateway, NULL))
2581 {
2582 ret = tun_adapter->Index;
2583 count = 1;
2584 on_tun = true;
2585 }
2586 else /* test on other interfaces */
2587 {
2588 count = test_route(adapters, r->gateway, &ret);
2589 }
2590
2591 if (count == 0)
2592 {
2593 msg(M_WARN, "Warning: route gateway is not reachable on any active network adapters: %s",
2594 print_in_addr_t(r->gateway, 0, &gc));
2596 }
2597 else if (count > 1)
2598 {
2599 msg(M_WARN, "Warning: route gateway is ambiguous: %s (%d matches)",
2600 print_in_addr_t(r->gateway, 0, &gc), count);
2601 }
2602
2603 dmsg(D_ROUTE_DEBUG, "DEBUG: route find if: on_tun=%d count=%d index=%lu",
2604 on_tun, count, ret);
2605
2606 gc_free(&gc);
2607 return ret;
2608}
2609
2610/* IPv6 implementation using GetBestRoute2()
2611 * https://msdn.microsoft.com/en-us/library/windows/desktop/aa365922(v=vs.85).aspx
2612 * https://msdn.microsoft.com/en-us/library/windows/desktop/aa814411(v=vs.85).aspx
2613 */
2614void
2615get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
2616 openvpn_net_ctx_t *ctx)
2617{
2618 struct gc_arena gc = gc_new();
2619 CLEAR(*rgi6);
2620
2621 SOCKADDR_INET DestinationAddress;
2622 CLEAR(DestinationAddress);
2623 DestinationAddress.si_family = AF_INET6;
2624 if (dest)
2625 {
2626 DestinationAddress.Ipv6.sin6_addr = *dest;
2627 }
2628
2629 MIB_IPFORWARD_ROW2 BestRoute;
2630 CLEAR(BestRoute);
2631 DWORD status = get_best_route(&gc, &DestinationAddress, &BestRoute);
2632
2633 if (status != NO_ERROR)
2634 {
2635 goto done;
2636 }
2637
2638 msg(D_ROUTE, "GDG6: II=%lu DP=%s/%d NH=%s", BestRoute.InterfaceIndex,
2639 print_in6_addr(BestRoute.DestinationPrefix.Prefix.Ipv6.sin6_addr, 0, &gc),
2640 BestRoute.DestinationPrefix.PrefixLength,
2641 print_in6_addr(BestRoute.NextHop.Ipv6.sin6_addr, 0, &gc));
2642 msg(D_ROUTE, "GDG6: Metric=%lu, Loopback=%u, AA=%u, I=%u", BestRoute.Metric,
2643 BestRoute.Loopback, BestRoute.AutoconfigureAddress, BestRoute.Immortal);
2644
2645 rgi6->gateway.addr_ipv6 = BestRoute.NextHop.Ipv6.sin6_addr;
2646 rgi6->adapter_index = BestRoute.InterfaceIndex;
2648
2649 /* on-link is signalled by receiving an empty (::) NextHop */
2650 if (IN6_IS_ADDR_UNSPECIFIED(&BestRoute.NextHop.Ipv6.sin6_addr))
2651 {
2652 rgi6->flags |= RGI_ON_LINK;
2653 }
2654
2655done:
2656 gc_free(&gc);
2657}
2658
2659/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2660static int
2661add_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt, DWORD adapter_index)
2662{
2663 struct gc_arena gc = gc_new();
2664 int ret = RTA_ERROR;
2665 DWORD status;
2666 const DWORD if_index = (adapter_index == TUN_ADAPTER_INDEX_INVALID)
2668 : adapter_index;
2669
2670 if (if_index != TUN_ADAPTER_INDEX_INVALID)
2671 {
2672 MIB_IPFORWARDROW fr;
2673 CLEAR(fr);
2674 fr.dwForwardDest = htonl(r->network);
2675 fr.dwForwardMask = htonl(r->netmask);
2676 fr.dwForwardPolicy = 0;
2677 fr.dwForwardNextHop = htonl(r->gateway);
2678 fr.dwForwardIfIndex = if_index;
2679 fr.dwForwardType = 4; /* the next hop is not the final dest */
2680 fr.dwForwardProto = 3; /* PROTO_IP_NETMGMT */
2681 fr.dwForwardAge = 0;
2682 fr.dwForwardNextHopAS = 0;
2683 fr.dwForwardMetric1 = (r->flags & RT_METRIC_DEFINED) ? r->metric : 1;
2684 fr.dwForwardMetric2 = METRIC_NOT_USED;
2685 fr.dwForwardMetric3 = METRIC_NOT_USED;
2686 fr.dwForwardMetric4 = METRIC_NOT_USED;
2687 fr.dwForwardMetric5 = METRIC_NOT_USED;
2688
2689 if ((r->network & r->netmask) != r->network)
2690 {
2691 msg(M_WARN, "Warning: address %s is not a network address in relation to netmask %s",
2692 print_in_addr_t(r->network, 0, &gc), print_in_addr_t(r->netmask, 0, &gc));
2693 }
2694
2695 status = CreateIpForwardEntry(&fr);
2696
2697 if (status == NO_ERROR)
2698 {
2699 ret = RTA_SUCCESS;
2700 }
2701 else if (status == ERROR_OBJECT_ALREADY_EXISTS)
2702 {
2703 ret = RTA_EEXIST;
2704 }
2705 else
2706 {
2707 /* failed, try increasing the metric to work around Vista issue */
2708 /* iteratively retry higher metrics up to this limit */
2709 const DWORD forward_metric_limit = 2048;
2710
2711 for (; fr.dwForwardMetric1 <= forward_metric_limit; ++fr.dwForwardMetric1)
2712 {
2713 /* try a different forward type=3 ("the next hop is the final dest") in addition
2714 * to 4.
2715 * --redirect-gateway over RRAS seems to need this. */
2716 for (fr.dwForwardType = 4; fr.dwForwardType >= 3; --fr.dwForwardType)
2717 {
2718 status = CreateIpForwardEntry(&fr);
2719 if (status == NO_ERROR)
2720 {
2721 msg(D_ROUTE,
2722 "ROUTE: CreateIpForwardEntry succeeded with dwForwardMetric1=%lu and dwForwardType=%lu",
2723 fr.dwForwardMetric1, fr.dwForwardType);
2724 ret = RTA_SUCCESS;
2725 goto doublebreak;
2726 }
2727 else if (status != ERROR_BAD_ARGUMENTS)
2728 {
2729 goto doublebreak;
2730 }
2731 }
2732 }
2733
2734doublebreak:
2735 if (status != NO_ERROR)
2736 {
2737 if (status == ERROR_OBJECT_ALREADY_EXISTS)
2738 {
2739 ret = RTA_EEXIST;
2740 }
2741 else
2742 {
2743 msg(M_WARN,
2744 "ERROR: route addition failed using CreateIpForwardEntry: "
2745 "%s [status=%lu if_index=%lu]",
2746 strerror_win32(status, &gc), status, if_index);
2747 }
2748 }
2749 }
2750 }
2751
2752 gc_free(&gc);
2753 return ret;
2754}
2755
2756static bool
2757del_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt)
2758{
2759 struct gc_arena gc = gc_new();
2760 bool ret = false;
2761 const DWORD if_index = windows_route_find_if_index(r, tt);
2762
2763 if (if_index != TUN_ADAPTER_INDEX_INVALID)
2764 {
2765 MIB_IPFORWARDROW fr;
2766 CLEAR(fr);
2767
2768 fr.dwForwardDest = htonl(r->network);
2769 fr.dwForwardMask = htonl(r->netmask);
2770 fr.dwForwardPolicy = 0;
2771 fr.dwForwardNextHop = htonl(r->gateway);
2772 fr.dwForwardIfIndex = if_index;
2773
2774 const DWORD status = DeleteIpForwardEntry(&fr);
2775 if (status == NO_ERROR)
2776 {
2777 ret = true;
2778 }
2779 else
2780 {
2781 msg(M_WARN, "ERROR: route deletion failed using DeleteIpForwardEntry: %s",
2783 }
2784 }
2785
2786 gc_free(&gc);
2787 return ret;
2788}
2789
2790/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2791static int
2792do_route_service(const bool add, const route_message_t *rt, const DWORD size, HANDLE pipe)
2793{
2794 int ret = RTA_ERROR;
2795 ack_message_t ack;
2796 struct gc_arena gc = gc_new();
2797
2798 if (!send_msg_iservice(pipe, rt, size, &ack, "ROUTE"))
2799 {
2800 goto out;
2801 }
2802
2803 if (ack.error_number != NO_ERROR)
2804 {
2805 ret = (ack.error_number == ERROR_OBJECT_ALREADY_EXISTS) ? RTA_EEXIST : RTA_ERROR;
2806 if (ret == RTA_ERROR)
2807 {
2808 msg(M_WARN, "ERROR: route %s failed using service: %s [status=%d if_index=%lu]",
2809 (add ? "addition" : "deletion"), strerror_win32(ack.error_number, &gc),
2810 ack.error_number, rt->iface.index);
2811 }
2812 goto out;
2813 }
2814
2815 ret = RTA_SUCCESS;
2816
2817out:
2818 gc_free(&gc);
2819 return ret;
2820}
2821
2822/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2823static int
2824do_route_ipv4_service(const bool add, const struct route_ipv4 *r, const struct tuntap *tt)
2825{
2826 DWORD if_index = windows_route_find_if_index(r, tt);
2827 if (if_index == TUN_ADAPTER_INDEX_INVALID)
2828 {
2829 return RTA_ERROR;
2830 }
2831
2832 route_message_t msg = { .header = { (add ? msg_add_route : msg_del_route),
2833 sizeof(route_message_t), 0 },
2834 .family = AF_INET,
2835 .prefix.ipv4.s_addr = htonl(r->network),
2836 .gateway.ipv4.s_addr = htonl(r->gateway),
2837 .iface = { .index = if_index, .name = "" },
2838 .metric = (r->flags & RT_METRIC_DEFINED) ? r->metric : -1 };
2839
2840 netmask_to_netbits(r->network, r->netmask, &msg.prefix_len);
2841 if (msg.prefix_len == -1)
2842 {
2843 msg.prefix_len = 32;
2844 }
2845
2846 return do_route_service(add, &msg, sizeof(msg), tt->options.msg_channel);
2847}
2848
2849/* Add or delete an ipv6 route
2850 * Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error
2851 */
2852static int
2853route_ipv6_ipapi(const bool add, const struct route_ipv6 *r, const struct tuntap *tt)
2854{
2855 DWORD err;
2856 int ret = RTA_ERROR;
2857 PMIB_IPFORWARD_ROW2 fwd_row;
2858 struct gc_arena gc = gc_new();
2859
2860 fwd_row = gc_malloc(sizeof(*fwd_row), true, &gc);
2861
2862 fwd_row->ValidLifetime = 0xffffffff;
2863 fwd_row->PreferredLifetime = 0xffffffff;
2864 fwd_row->Protocol = MIB_IPPROTO_NETMGMT;
2865 fwd_row->Metric = ((r->flags & RT_METRIC_DEFINED) ? r->metric : -1);
2866 fwd_row->DestinationPrefix.Prefix.si_family = AF_INET6;
2867 fwd_row->DestinationPrefix.Prefix.Ipv6.sin6_addr = r->network;
2868 fwd_row->DestinationPrefix.PrefixLength = (UINT8)r->netbits;
2869 fwd_row->NextHop.si_family = AF_INET6;
2870 fwd_row->NextHop.Ipv6.sin6_addr = r->gateway;
2871 fwd_row->InterfaceIndex = r->adapter_index ? r->adapter_index : tt->adapter_index;
2872
2873 /* In TUN mode we use a special link-local address as the next hop.
2874 * The tapdrvr knows about it and will answer neighbor discovery packets.
2875 * (only do this for routes actually using the tun/tap device)
2876 */
2877 if (tt->type == DEV_TYPE_TUN && !r->adapter_index)
2878 {
2879 inet_pton(AF_INET6, "fe80::8", &fwd_row->NextHop.Ipv6.sin6_addr);
2880 }
2881
2882 /* Use LUID if interface index not available */
2883 if (fwd_row->InterfaceIndex == TUN_ADAPTER_INDEX_INVALID && strlen(tt->actual_name))
2884 {
2885 NET_LUID luid;
2886 err = ConvertInterfaceAliasToLuid(wide_string(tt->actual_name, &gc), &luid);
2887 if (err != NO_ERROR)
2888 {
2889 goto out;
2890 }
2891 fwd_row->InterfaceLuid = luid;
2892 fwd_row->InterfaceIndex = 0;
2893 }
2894
2895 if (add)
2896 {
2897 err = CreateIpForwardEntry2(fwd_row);
2898 }
2899 else
2900 {
2901 err = DeleteIpForwardEntry2(fwd_row);
2902 }
2903
2904out:
2905 if (err != NO_ERROR)
2906 {
2907 ret = (err == ERROR_OBJECT_ALREADY_EXISTS) ? RTA_EEXIST : RTA_ERROR;
2908 if (ret == RTA_ERROR)
2909 {
2910 msg(M_WARN, "ERROR: route %s failed using ipapi: %s [status=%lu if_index=%lu]",
2911 (add ? "addition" : "deletion"), strerror_win32(err, &gc), err,
2912 fwd_row->InterfaceIndex);
2913 }
2914 else if (add)
2915 {
2916 msg(D_ROUTE, "IPv6 route addition using ipapi failed because route exists");
2917 }
2918 }
2919 else
2920 {
2921 msg(D_ROUTE, "IPv6 route %s using ipapi", add ? "added" : "deleted");
2922 ret = RTA_SUCCESS;
2923 }
2924 gc_free(&gc);
2925 return ret;
2926}
2927
2928/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2929static int
2930do_route_ipv6_service(const bool add, const struct route_ipv6 *r, const struct tuntap *tt)
2931{
2932 int status;
2933 route_message_t msg = { .header = { (add ? msg_add_route : msg_del_route),
2934 sizeof(route_message_t), 0 },
2935 .family = AF_INET6,
2936 .prefix.ipv6 = r->network,
2937 .prefix_len = r->netbits,
2938 .gateway.ipv6 = r->gateway,
2939 .iface = { .index = tt->adapter_index, .name = "" },
2940 .metric = (r->flags & RT_METRIC_DEFINED) ? r->metric : -1 };
2941
2942 if (r->adapter_index) /* vpn server special route */
2943 {
2944 msg.iface.index = r->adapter_index;
2945 }
2946
2947 /* In TUN mode we use a special link-local address as the next hop.
2948 * The tapdrvr knows about it and will answer neighbor discovery packets.
2949 * (only do this for routes actually using the tun/tap device)
2950 */
2951 if (tt->type == DEV_TYPE_TUN && msg.iface.index == tt->adapter_index)
2952 {
2953 inet_pton(AF_INET6, "fe80::8", &msg.gateway.ipv6);
2954 }
2955
2956 if (msg.iface.index == TUN_ADAPTER_INDEX_INVALID)
2957 {
2958 strlcpy(msg.iface.name, tt->actual_name, sizeof(msg.iface.name));
2959 }
2960
2961 status = do_route_service(add, &msg, sizeof(msg), tt->options.msg_channel);
2962 if (status != RTA_ERROR)
2963 {
2964 msg(D_ROUTE, "IPv6 route %s via service %s", add ? "addition" : "deletion",
2965 (status == RTA_SUCCESS) ? "succeeded" : "failed because route exists");
2966 }
2967 return status;
2968}
2969
2970/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2971static int
2972add_route_service(const struct route_ipv4 *r, const struct tuntap *tt)
2973{
2974 return do_route_ipv4_service(true, r, tt);
2975}
2976
2977static bool
2978del_route_service(const struct route_ipv4 *r, const struct tuntap *tt)
2979{
2980 return do_route_ipv4_service(false, r, tt);
2981}
2982
2983/* Returns RTA_SUCCESS on success, RTA_EEXIST if route exists, RTA_ERROR on error */
2984static int
2985add_route_ipv6_service(const struct route_ipv6 *r, const struct tuntap *tt)
2986{
2987 return do_route_ipv6_service(true, r, tt);
2988}
2989
2990static bool
2991del_route_ipv6_service(const struct route_ipv6 *r, const struct tuntap *tt)
2992{
2993 return do_route_ipv6_service(false, r, tt);
2994}
2995
2996static const char *
2997format_route_entry(const MIB_IPFORWARDROW *r, struct gc_arena *gc)
2998{
2999 struct buffer out = alloc_buf_gc(256, gc);
3000 buf_printf(&out, "%s %s %s p=%lu i=%lu t=%lu pr=%lu a=%lu h=%lu m=%lu/%lu/%lu/%lu/%lu",
3001 print_in_addr_t(r->dwForwardDest, IA_NET_ORDER, gc),
3002 print_in_addr_t(r->dwForwardMask, IA_NET_ORDER, gc),
3003 print_in_addr_t(r->dwForwardNextHop, IA_NET_ORDER, gc), r->dwForwardPolicy,
3004 r->dwForwardIfIndex, r->dwForwardType, r->dwForwardProto,
3005 r->dwForwardAge, r->dwForwardNextHopAS, r->dwForwardMetric1,
3006 r->dwForwardMetric2, r->dwForwardMetric3, r->dwForwardMetric4,
3007 r->dwForwardMetric5);
3008 return BSTR(&out);
3009}
3010
3011/*
3012 * Show current routing table
3013 */
3014void
3016{
3017 struct gc_arena gc = gc_new();
3018
3019 const MIB_IPFORWARDTABLE *rt = get_windows_routing_table(&gc);
3020
3021 msg(msglevel, "SYSTEM ROUTING TABLE");
3022 if (rt)
3023 {
3024 for (DWORD i = 0; i < rt->dwNumEntries; ++i)
3025 {
3026 msg(msglevel, "%s", format_route_entry(&rt->table[i], &gc));
3027 }
3028 }
3029 gc_free(&gc);
3030}
3031
3032#elif defined(TARGET_ANDROID)
3033
3034void
3036{
3037 /* Android, set some pseudo GW, addr is in host byte order,
3038 * Determining the default GW on Android 5.0+ is non trivial
3039 * and serves almost no purpose since OpenVPN only uses the
3040 * default GW address to add routes for networks that should
3041 * NOT be routed over the VPN. Using a well known address
3042 * (127.'d'.'g'.'w') for the default GW make detecting
3043 * these routes easier from the controlling app.
3044 */
3045 CLEAR(*rgi);
3046
3047 rgi->gateway.addr = 127 << 24 | 'd' << 16 | 'g' << 8 | 'w';
3049 strcpy(rgi->iface, "android-gw");
3050
3051 /* Skip scanning/fetching interface from loopback interface we do
3052 * normally on Linux.
3053 * It always fails and "ioctl(SIOCGIFCONF) failed" confuses users
3054 */
3055}
3056
3057void
3058get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
3059 openvpn_net_ctx_t *ctx)
3060{
3061 /* Same for ipv6 */
3062
3063 CLEAR(*rgi6);
3064
3065 /* Use a fake link-local address */
3066 ASSERT(inet_pton(AF_INET6, "fe80::ad", &rgi6->addrs->addr_ipv6) == 1);
3067 rgi6->addrs->netbits_ipv6 = 64;
3069 strcpy(rgi6->iface, "android-gw");
3070}
3071
3072#elif defined(TARGET_LINUX)
3073
3074void
3076{
3077 int sd = -1;
3078 char best_name[IFNAMSIZ];
3079
3080 CLEAR(*rgi);
3081 CLEAR(best_name);
3082
3083 /* find best route to 'dest', get gateway IP addr + interface */
3084 if (net_route_v4_best_gw(ctx, &dest, &rgi->gateway.addr, best_name) == 0)
3085 {
3086 rgi->flags |= RGI_ADDR_DEFINED;
3087 if (!rgi->gateway.addr && best_name[0])
3088 {
3089 rgi->flags |= RGI_ON_LINK;
3090 }
3091 }
3092
3093 /* scan adapter list */
3094 if (rgi->flags & RGI_ADDR_DEFINED)
3095 {
3096 const struct ifreq *ifr;
3097 const struct ifreq *ifend;
3098 in_addr_t addr, netmask;
3099 struct ifreq ifreq;
3100 struct ifconf ifc;
3101 struct ifreq ifs[20]; /* Maximum number of interfaces to scan */
3102
3103 if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
3104 {
3105 msg(M_WARN, "GDG: socket() failed");
3106 goto done;
3107 }
3108 ifc.ifc_len = sizeof(ifs);
3109 ifc.ifc_req = ifs;
3110 if (ioctl(sd, SIOCGIFCONF, &ifc) < 0)
3111 {
3112 msg(M_WARN, "GDG: ioctl(SIOCGIFCONF) failed");
3113 goto done;
3114 }
3115
3116 /* scan through interface list */
3117 ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
3118 for (ifr = ifc.ifc_req; ifr < ifend; ifr++)
3119 {
3120 if (ifr->ifr_addr.sa_family == AF_INET)
3121 {
3122 /* get interface addr */
3123 addr = ntohl(((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr);
3124
3125 /* get interface name */
3126 strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
3127
3128 /* check that the interface is up */
3129 if (ioctl(sd, SIOCGIFFLAGS, &ifreq) < 0)
3130 {
3131 continue;
3132 }
3133 if (!(ifreq.ifr_flags & IFF_UP))
3134 {
3135 continue;
3136 }
3137
3138 if (rgi->flags & RGI_ON_LINK)
3139 {
3140 /* check that interface name of current interface
3141 * matches interface name of best default route */
3142 if (strcmp(ifreq.ifr_name, best_name))
3143 {
3144 continue;
3145 }
3146#if 0
3147 /* if point-to-point link, use remote addr as route gateway */
3148 if ((ifreq.ifr_flags & IFF_POINTOPOINT) && ioctl(sd, SIOCGIFDSTADDR, &ifreq) >= 0)
3149 {
3150 rgi->gateway.addr = ntohl(((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr);
3151 if (rgi->gateway.addr)
3152 {
3153 rgi->flags &= ~RGI_ON_LINK;
3154 }
3155 }
3156#endif
3157 }
3158 else
3159 {
3160 /* get interface netmask */
3161 if (ioctl(sd, SIOCGIFNETMASK, &ifreq) < 0)
3162 {
3163 continue;
3164 }
3165 netmask = ntohl(((struct sockaddr_in *)&ifreq.ifr_addr)->sin_addr.s_addr);
3166
3167 /* check that interface matches default route */
3168 if (((rgi->gateway.addr ^ addr) & netmask) != 0)
3169 {
3170 continue;
3171 }
3172
3173 /* save netmask */
3174 rgi->gateway.netmask = netmask;
3175 rgi->flags |= RGI_NETMASK_DEFINED;
3176 }
3177
3178 /* save iface name */
3179 strlcpy(rgi->iface, ifreq.ifr_name, sizeof(rgi->iface));
3180 rgi->flags |= RGI_IFACE_DEFINED;
3181
3182 /* now get the hardware address. */
3183 memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
3184 if (ioctl(sd, SIOCGIFHWADDR, &ifreq) < 0)
3185 {
3186 msg(M_WARN, "GDG: SIOCGIFHWADDR(%s) failed", ifreq.ifr_name);
3187 goto done;
3188 }
3189 memcpy(rgi->hwaddr, &ifreq.ifr_hwaddr.sa_data, 6);
3190 rgi->flags |= RGI_HWADDR_DEFINED;
3191
3192 break;
3193 }
3194 }
3195 }
3196
3197done:
3198 if (sd >= 0)
3199 {
3200 close(sd);
3201 }
3202}
3203
3204#if defined(__GNUC__) || defined(__clang__)
3205#pragma GCC diagnostic pop
3206#endif
3207
3208/* IPv6 implementation using netlink
3209 * https://www.linuxjournal.com/article/7356 - "Kernel Korner - Why and How to Use Netlink Socket"
3210 * netlink(3), netlink(7), rtnetlink(7)
3211 * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/NetworkServices/NAT/
3212 */
3213
3214void
3215get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
3216 openvpn_net_ctx_t *ctx)
3217{
3218 int flags;
3219
3220 CLEAR(*rgi6);
3221
3222 if (net_route_v6_best_gw(ctx, dest, &rgi6->gateway.addr_ipv6, rgi6->iface) == 0)
3223 {
3224 if (!IN6_IS_ADDR_UNSPECIFIED(&rgi6->gateway.addr_ipv6))
3225 {
3226 rgi6->flags |= RGI_ADDR_DEFINED;
3227 }
3228
3229 if (strlen(rgi6->iface) > 0)
3230 {
3231 rgi6->flags |= RGI_IFACE_DEFINED;
3232 }
3233 }
3234
3235 /* if we have an interface but no gateway, the destination is on-link */
3236 flags = rgi6->flags & (RGI_IFACE_DEFINED | RGI_ADDR_DEFINED);
3237 if (flags == RGI_IFACE_DEFINED)
3238 {
3239 rgi6->flags |= (RGI_ADDR_DEFINED | RGI_ON_LINK);
3240 if (dest)
3241 {
3242 rgi6->gateway.addr_ipv6 = *dest;
3243 }
3244 }
3245}
3246
3247#elif defined(TARGET_DARWIN) || defined(TARGET_SOLARIS) || defined(TARGET_FREEBSD) \
3248 || defined(TARGET_DRAGONFLY) || defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
3249
3250#include <sys/types.h>
3251#include <sys/socket.h>
3252#include <netinet/in.h>
3253#include <net/route.h>
3254#include <net/if_dl.h>
3255#if !defined(TARGET_SOLARIS)
3256#include <ifaddrs.h>
3257#endif
3258
3259struct rtmsg
3260{
3261 struct rt_msghdr m_rtm;
3262 char m_space[512];
3263};
3264
3265/* the route socket code is identical for all 4 supported BSDs and for
3266 * MacOS X (Darwin), with one crucial difference: when going from
3267 * 32 bit to 64 bit, FreeBSD/OpenBSD increased the structure size but kept
3268 * source code compatibility by keeping the use of "long", while
3269 * MacOS X decided to keep binary compatibility by *changing* the API
3270 * to use "uint32_t", thus 32 bit on all OS X variants
3271 *
3272 * NetBSD does the MacOS way of "fixed number of bits, no matter if
3273 * 32 or 64 bit OS", but chose uint64_t. For maximum portability, we
3274 * just use the OS RT_ROUNDUP() macro, which is guaranteed to be correct.
3275 *
3276 * We used to have a large amount of duplicate code here which really
3277 * differed only in this (long) vs. (uint32_t) - IMHO, worse than
3278 * having a combined block for all BSDs with this single #ifdef inside
3279 */
3280
3281#if defined(TARGET_DARWIN)
3282#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
3283#elif defined(TARGET_NETBSD)
3284#define ROUNDUP(a) RT_ROUNDUP(a)
3285#else
3286#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
3287#endif
3288
3289#if defined(TARGET_SOLARIS)
3290#define NEXTADDR(w, u) \
3291 if (rtm_addrs & (w)) \
3292 { \
3293 size_t l = sizeof(u); \
3294 memmove(cp, &(u), l); \
3295 cp += ROUNDUP(l); \
3296 }
3297
3298#define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr_in)))
3299#else /* if defined(TARGET_SOLARIS) */
3300#define NEXTADDR(w, u) \
3301 if (rtm_addrs & (w)) \
3302 { \
3303 size_t l = ((struct sockaddr *)&(u))->sa_len; \
3304 memmove(cp, &(u), l); \
3305 cp += ROUNDUP(l); \
3306 }
3307
3308#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
3309#endif
3310
3311void
3313{
3314 struct rtmsg m_rtmsg;
3315 int sockfd = -1;
3316 int rtm_addrs;
3317 struct sockaddr so_dst, so_mask;
3318 char *cp = m_rtmsg.m_space;
3319 struct rt_msghdr *rtm_aux;
3320
3321#define rtm m_rtmsg.m_rtm
3322
3323 CLEAR(*rgi);
3324
3325 /* setup data to send to routing socket */
3326 const int pid = getpid();
3327 int seq = 0;
3328#ifdef TARGET_OPENBSD
3329 rtm_addrs = RTA_DST | RTA_NETMASK; /* Kernel refuses RTA_IFP */
3330#else
3331 rtm_addrs = RTA_DST | RTA_NETMASK | RTA_IFP;
3332#endif
3333
3334 CLEAR(m_rtmsg);
3335 CLEAR(so_dst);
3336 CLEAR(so_mask);
3337 CLEAR(rtm);
3338
3339 rtm.rtm_type = RTM_GET;
3340 rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
3341 rtm.rtm_version = RTM_VERSION;
3342 rtm.rtm_seq = ++seq;
3343#ifdef TARGET_OPENBSD
3344 rtm.rtm_tableid = (u_short)getrtable();
3345#endif
3346 rtm.rtm_addrs = rtm_addrs;
3347
3348 so_dst.sa_family = AF_INET;
3349 so_mask.sa_family = AF_INET;
3350
3351#ifndef TARGET_SOLARIS
3352 so_dst.sa_len = sizeof(struct sockaddr_in);
3353 so_mask.sa_len = sizeof(struct sockaddr_in);
3354#endif
3355
3356 NEXTADDR(RTA_DST, so_dst);
3357 NEXTADDR(RTA_NETMASK, so_mask);
3358
3359 /* sizeof(struct rt_msghdr) + padding */
3360 rtm.rtm_msglen = (u_short)(cp - (char *)&m_rtmsg);
3361
3362 /* transact with routing socket */
3363 sockfd = socket(PF_ROUTE, SOCK_RAW, 0);
3364 if (sockfd < 0)
3365 {
3366 msg(M_WARN, "GDG: socket #1 failed");
3367 goto done;
3368 }
3369 if (write(sockfd, (char *)&m_rtmsg, rtm.rtm_msglen) < 0)
3370 {
3371 msg(M_WARN | M_ERRNO, "GDG: problem writing to routing socket");
3372 goto done;
3373 }
3374 ssize_t ret;
3375 do
3376 {
3377 ret = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
3378 } while (ret > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
3379 close(sockfd);
3380 sockfd = -1;
3381
3382 /* extract return data from routing socket */
3383 const struct sockaddr_in *gate = NULL;
3384 const struct sockaddr_dl *adl = NULL;
3385 rtm_aux = &rtm;
3386 cp = (char *)(rtm_aux + 1);
3387 if (rtm_aux->rtm_addrs)
3388 {
3389 for (unsigned int i = 1; i; i <<= 1)
3390 {
3391 if (i & rtm_aux->rtm_addrs)
3392 {
3393 const struct sockaddr *sa = (const struct sockaddr *)cp;
3394 if (i == RTA_GATEWAY)
3395 {
3396 gate = (const struct sockaddr_in *)sa;
3397 }
3398 else if (i == RTA_IFP)
3399 {
3400 adl = (const struct sockaddr_dl *)sa;
3401 }
3402 ADVANCE(cp, sa);
3403 }
3404 }
3405 }
3406 else
3407 {
3408 goto done;
3409 }
3410
3411 /* get gateway addr and interface name */
3412 if (gate)
3413 {
3414 /* get default gateway addr */
3415 rgi->gateway.addr = ntohl((gate)->sin_addr.s_addr);
3416 if (rgi->gateway.addr)
3417 {
3418 rgi->flags |= RGI_ADDR_DEFINED;
3419 }
3420
3421 if (adl)
3422 {
3423 /* get interface name */
3424 if (adl->sdl_nlen && adl->sdl_nlen < sizeof(rgi->iface))
3425 {
3426 memcpy(rgi->iface, adl->sdl_data, adl->sdl_nlen);
3427 rgi->iface[adl->sdl_nlen] = '\0';
3428 rgi->flags |= RGI_IFACE_DEFINED;
3429 }
3430 }
3431 }
3432
3433 /* get netmask of interface that owns default gateway */
3434 if (rgi->flags & RGI_IFACE_DEFINED)
3435 {
3436 struct ifreq ifr;
3437
3438 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
3439 if (sockfd < 0)
3440 {
3441 msg(M_WARN, "GDG: socket #2 failed");
3442 goto done;
3443 }
3444
3445 CLEAR(ifr);
3446 ifr.ifr_addr.sa_family = AF_INET;
3447 strlcpy(ifr.ifr_name, rgi->iface, IFNAMSIZ);
3448
3449 if (ioctl(sockfd, SIOCGIFNETMASK, (char *)&ifr) < 0)
3450 {
3451 msg(M_WARN, "GDG: ioctl #1 failed");
3452 goto done;
3453 }
3454 close(sockfd);
3455 sockfd = -1;
3456
3457 rgi->gateway.netmask = ntohl(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
3458 rgi->flags |= RGI_NETMASK_DEFINED;
3459 }
3460
3461 /* try to read MAC addr associated with interface that owns default gateway */
3462 if (rgi->flags & RGI_IFACE_DEFINED)
3463 {
3464#if defined(TARGET_SOLARIS)
3465 /* OpenSolaris has getifaddrs(3), but it does not return AF_LINK */
3466 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
3467 if (sockfd < 0)
3468 {
3469 msg(M_WARN, "GDG: socket #3 failed");
3470 goto done;
3471 }
3472
3473 struct ifreq ifreq = { 0 };
3474
3475 /* now get the hardware address. */
3476 strlcpy(ifreq.ifr_name, rgi->iface, sizeof(ifreq.ifr_name));
3477 if (ioctl(sockfd, SIOCGIFHWADDR, &ifreq) < 0)
3478 {
3479 msg(M_WARN, "GDG: SIOCGIFHWADDR(%s) failed", ifreq.ifr_name);
3480 }
3481 else
3482 {
3483 memcpy(rgi->hwaddr, &ifreq.ifr_addr.sa_data, 6);
3484 rgi->flags |= RGI_HWADDR_DEFINED;
3485 }
3486#else /* if defined(TARGET_SOLARIS) */
3487 struct ifaddrs *ifap, *ifa;
3488
3489 if (getifaddrs(&ifap) != 0)
3490 {
3491 msg(M_WARN | M_ERRNO, "GDG: getifaddrs() failed");
3492 goto done;
3493 }
3494
3495 for (ifa = ifap; ifa; ifa = ifa->ifa_next)
3496 {
3497 if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == AF_LINK
3498 && !strncmp(ifa->ifa_name, rgi->iface, IFNAMSIZ))
3499 {
3500 struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr;
3501 memcpy(rgi->hwaddr, LLADDR(sdl), 6);
3502 rgi->flags |= RGI_HWADDR_DEFINED;
3503 }
3504 }
3505
3506 freeifaddrs(ifap);
3507#endif /* if defined(TARGET_SOLARIS) */
3508 }
3509
3510done:
3511 if (sockfd >= 0)
3512 {
3513 close(sockfd);
3514 }
3515}
3516
3517/* BSD implementation using routing socket (as does IPv4)
3518 * (the code duplication is somewhat unavoidable if we want this to
3519 * work on OpenSolaris as well. *sigh*)
3520 */
3521
3522/* Solaris has no length field - this is ugly, but less #ifdef in total
3523 */
3524#if defined(TARGET_SOLARIS)
3525#undef ADVANCE
3526#define ADVANCE(x, n) (x += ROUNDUP(sizeof(struct sockaddr_in6)))
3527#endif
3528
3529void
3530get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
3531 openvpn_net_ctx_t *ctx)
3532{
3533 struct rtmsg m_rtmsg;
3534 int sockfd = -1;
3535 int rtm_addrs;
3536 struct sockaddr_in6 so_dst, so_mask;
3537 char *cp = m_rtmsg.m_space;
3538 struct sockaddr *gate = NULL, *ifp = NULL, *sa;
3539 struct rt_msghdr *rtm_aux;
3540
3541 CLEAR(*rgi6);
3542
3543 /* setup data to send to routing socket */
3544 const int pid = getpid();
3545 int seq = 0;
3546#ifdef TARGET_OPENBSD
3547 rtm_addrs = RTA_DST | RTA_NETMASK; /* Kernel refuses RTA_IFP */
3548#else
3549 rtm_addrs = RTA_DST | RTA_NETMASK | RTA_IFP;
3550#endif
3551
3552 CLEAR(m_rtmsg);
3553 CLEAR(so_dst);
3554 CLEAR(so_mask);
3555 CLEAR(rtm);
3556
3557 rtm.rtm_type = RTM_GET;
3558 rtm.rtm_flags = RTF_UP;
3559 rtm.rtm_version = RTM_VERSION;
3560 rtm.rtm_seq = ++seq;
3561#ifdef TARGET_OPENBSD
3562 rtm.rtm_tableid = (u_short)getrtable();
3563#endif
3564
3565 so_dst.sin6_family = AF_INET6;
3566 so_mask.sin6_family = AF_INET6;
3567
3568 if (dest != NULL /* specific host? */
3569 && !IN6_IS_ADDR_UNSPECIFIED(dest))
3570 {
3571 so_dst.sin6_addr = *dest;
3572 /* :: needs /0 "netmask", host route wants "no netmask */
3573 rtm_addrs &= ~RTA_NETMASK;
3574 }
3575
3576 rtm.rtm_addrs = rtm_addrs;
3577
3578#ifndef TARGET_SOLARIS
3579 so_dst.sin6_len = sizeof(struct sockaddr_in6);
3580 so_mask.sin6_len = sizeof(struct sockaddr_in6);
3581#endif
3582
3583 NEXTADDR(RTA_DST, so_dst);
3584 NEXTADDR(RTA_NETMASK, so_mask);
3585
3586 /* sizeof(struct rt_msghdr) + padding */
3587 rtm.rtm_msglen = (u_short)(cp - (char *)&m_rtmsg);
3588
3589 /* transact with routing socket */
3590 sockfd = socket(PF_ROUTE, SOCK_RAW, 0);
3591 if (sockfd < 0)
3592 {
3593 msg(M_WARN, "GDG6: socket #1 failed");
3594 goto done;
3595 }
3596 if (write(sockfd, (char *)&m_rtmsg, rtm.rtm_msglen) < 0)
3597 {
3598 msg(M_WARN | M_ERRNO, "GDG6: problem writing to routing socket");
3599 goto done;
3600 }
3601 ssize_t ret;
3602 do
3603 {
3604 ret = read(sockfd, (char *)&m_rtmsg, sizeof(m_rtmsg));
3605 } while (ret > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
3606
3607 close(sockfd);
3608 sockfd = -1;
3609
3610 /* extract return data from routing socket */
3611 rtm_aux = &rtm;
3612 cp = (char *)(rtm_aux + 1);
3613 if (rtm_aux->rtm_addrs)
3614 {
3615 for (unsigned int i = 1; i; i <<= 1)
3616 {
3617 if (i & rtm_aux->rtm_addrs)
3618 {
3619 sa = (struct sockaddr *)cp;
3620 if (i == RTA_GATEWAY)
3621 {
3622 gate = sa;
3623 }
3624 else if (i == RTA_IFP)
3625 {
3626 ifp = sa;
3627 }
3628 ADVANCE(cp, sa);
3629 }
3630 }
3631 }
3632 else
3633 {
3634 goto done;
3635 }
3636
3637 /* get gateway addr and interface name */
3638 if (gate != NULL)
3639 {
3640 const struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)gate;
3641 struct in6_addr gw = s6->sin6_addr;
3642
3643#ifndef TARGET_SOLARIS
3644 /* You do not really want to know... from FreeBSD's route.c
3645 * (KAME encodes the 16 bit scope_id in s6_addr[2] + [3],
3646 * but for a correct link-local address these must be :0000: )
3647 */
3648 if (gate->sa_len == sizeof(struct sockaddr_in6) && IN6_IS_ADDR_LINKLOCAL(&gw))
3649 {
3650 gw.s6_addr[2] = gw.s6_addr[3] = 0;
3651 }
3652
3653 if (gate->sa_len != sizeof(struct sockaddr_in6) || IN6_IS_ADDR_UNSPECIFIED(&gw))
3654 {
3655 rgi6->flags |= RGI_ON_LINK;
3656 }
3657 else
3658#endif
3659 {
3660 rgi6->gateway.addr_ipv6 = gw;
3661 }
3662 rgi6->flags |= RGI_ADDR_DEFINED;
3663
3664 if (ifp)
3665 {
3666 /* get interface name */
3667 const struct sockaddr_dl *adl = (struct sockaddr_dl *)ifp;
3668 if (adl->sdl_nlen && adl->sdl_nlen < sizeof(rgi6->iface))
3669 {
3670 memcpy(rgi6->iface, adl->sdl_data, adl->sdl_nlen);
3671 rgi6->flags |= RGI_IFACE_DEFINED;
3672 }
3673 }
3674 }
3675
3676done:
3677 if (sockfd >= 0)
3678 {
3679 close(sockfd);
3680 }
3681}
3682
3683#elif defined(TARGET_HAIKU)
3684
3685void
3687{
3688 CLEAR(*rgi);
3689
3690 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
3691 if (sockfd < 0)
3692 {
3693 msg(M_ERRNO, "%s: Error opening socket for AF_INET", __func__);
3694 return;
3695 }
3696
3697 struct ifconf config;
3698 config.ifc_len = sizeof(config.ifc_value);
3699 if (ioctl(sockfd, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
3700 {
3701 msg(M_ERRNO, "%s: Error getting routing table size", __func__);
3702 close(sockfd);
3703 return;
3704 }
3705
3706 uint32 size = (uint32)config.ifc_value;
3707 if (size == 0)
3708 {
3709 close(sockfd);
3710 return;
3711 }
3712
3713 void *buffer = malloc(size);
3715
3716 config.ifc_len = size;
3717 config.ifc_buf = buffer;
3718 int ret = ioctl(sockfd, SIOCGRTTABLE, &config, sizeof(struct ifconf));
3719 close(sockfd);
3720 if (ret < 0)
3721 {
3722 free(buffer);
3723 return;
3724 }
3725
3726 struct ifreq *interface = (struct ifreq *)buffer;
3727 struct ifreq *end = (struct ifreq *)((uint8 *)buffer + size);
3728
3729 while (interface < end)
3730 {
3731 struct route_entry route = interface->ifr_route;
3732 if ((route.flags & RTF_GATEWAY) != 0 && (route.flags & RTF_DEFAULT) != 0)
3733 {
3734 rgi->gateway.addr = ntohl(((struct sockaddr_in *)route.gateway)->sin_addr.s_addr);
3736 strlcpy(rgi->iface, interface->ifr_name, sizeof(rgi->iface));
3737 }
3738
3739 int32 address_size = 0;
3740 if (route.destination != NULL)
3741 {
3742 address_size += route.destination->sa_len;
3743 }
3744 if (route.mask != NULL)
3745 {
3746 address_size += route.mask->sa_len;
3747 }
3748 if (route.gateway != NULL)
3749 {
3750 address_size += route.gateway->sa_len;
3751 }
3752
3753 interface = (struct ifreq *)((addr_t)interface + IF_NAMESIZE + sizeof(struct route_entry)
3754 + address_size);
3755 }
3756 free(buffer);
3757}
3758
3759void
3760get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
3761 openvpn_net_ctx_t *ctx)
3762{
3763 /* TODO: Same for ipv6 with AF_INET6 */
3764 CLEAR(*rgi6);
3765}
3766
3767#else /* if defined(_WIN32) */
3768
3769/*
3770 * This is a platform-specific method that returns data about
3771 * the current default gateway. Return data is placed into
3772 * a struct route_gateway_info object provided by caller. The
3773 * implementation should CLEAR the structure before adding
3774 * data to it.
3775 *
3776 * Data returned includes:
3777 * 1. default gateway address (rgi->gateway.addr)
3778 * 2. netmask of interface that owns default gateway
3779 * (rgi->gateway.netmask)
3780 * 3. hardware address (i.e. MAC address) of interface that owns
3781 * default gateway (rgi->hwaddr)
3782 * 4. interface name (or adapter index on Windows) that owns default
3783 * gateway (rgi->iface or rgi->adapter_index)
3784 * 5. an array of additional address/netmask pairs defined by
3785 * interface that owns default gateway (rgi->addrs with length
3786 * given in rgi->n_addrs)
3787 *
3788 * The flags RGI_x_DEFINED may be used to indicate which of the data
3789 * members were successfully returned (set in rgi->flags). All of
3790 * the data members are optional, however certain OpenVPN functionality
3791 * may be disabled by missing items.
3792 */
3793void
3795{
3796 CLEAR(*rgi);
3797}
3798void
3799get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest,
3800 openvpn_net_ctx_t *ctx)
3801{
3802 msg(D_ROUTE, "no support for get_default_gateway_ipv6() on this system");
3803 CLEAR(*rgi6);
3804}
3805
3806#endif /* if defined(_WIN32) */
3807
3808bool
3809netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
3810{
3811 const int addrlen = sizeof(in_addr_t) * 8;
3812
3813 if ((network & netmask) == network)
3814 {
3815 for (int i = 0; i <= addrlen; ++i)
3816 {
3817 in_addr_t mask = netbits_to_netmask(i);
3818 if (mask == netmask)
3819 {
3820 if (i == addrlen)
3821 {
3822 *netbits = -1;
3823 }
3824 else
3825 {
3826 *netbits = i;
3827 }
3828 return true;
3829 }
3830 }
3831 }
3832 return false;
3833}
3834
3835/* similar to netmask_to_netbits(), but don't mess with base address
3836 * etc., just convert to netbits - non-mappable masks are returned as "-1"
3837 */
3838int
3840{
3841 int i;
3842 const int addrlen = sizeof(in_addr_t) * 8;
3843
3844 for (i = 0; i <= addrlen; ++i)
3845 {
3846 in_addr_t mask = netbits_to_netmask(i);
3847 if (mask == netmask)
3848 {
3849 return i;
3850 }
3851 }
3852 return -1;
3853}
3854
3855
3856/*
3857 * get_bypass_addresses() is used by the redirect-gateway bypass-x
3858 * functions to build a route bypass to selected DHCP/DNS servers,
3859 * so that outgoing packets to these servers don't end up in the tunnel.
3860 */
3861
3862#if defined(_WIN32)
3863
3864static void
3866{
3867 if (test_local_addr(addr, NULL) == TLA_NONLOCAL && addr != 0 && addr != IPV4_NETMASK_HOST)
3868 {
3869 add_bypass_address(rb, addr);
3870 }
3871}
3872
3873static void
3874add_host_route_array(struct route_bypass *rb, const IP_ADDR_STRING *iplist)
3875{
3876 while (iplist)
3877 {
3878 bool succeed = false;
3879 const in_addr_t ip =
3880 getaddr(GETADDR_HOST_ORDER, iplist->IpAddress.String, 0, &succeed, NULL);
3881 if (succeed)
3882 {
3884 }
3885 iplist = iplist->Next;
3886 }
3887}
3888
3889static void
3890get_bypass_addresses(struct route_bypass *rb, const unsigned int flags)
3891{
3892 struct gc_arena gc = gc_new();
3893 /*bool ret_bool = false;*/
3894
3895 /* get full routing table */
3896 const MIB_IPFORWARDTABLE *routes = get_windows_routing_table(&gc);
3897
3898 /* get the route which represents the default gateway */
3899 const MIB_IPFORWARDROW *row = get_default_gateway_row(routes);
3900
3901 if (row)
3902 {
3903 /* get the adapter which the default gateway is associated with */
3904 const IP_ADAPTER_INFO *dgi = get_adapter_info(row->dwForwardIfIndex, &gc);
3905
3906 /* get extra adapter info, such as DNS addresses */
3907 const IP_PER_ADAPTER_INFO *pai = get_per_adapter_info(row->dwForwardIfIndex, &gc);
3908
3909 /* Bypass DHCP server address */
3910 if ((flags & RG_BYPASS_DHCP) && dgi && dgi->DhcpEnabled)
3911 {
3912 add_host_route_array(rb, &dgi->DhcpServer);
3913 }
3914
3915 /* Bypass DNS server addresses */
3916 if ((flags & RG_BYPASS_DNS) && pai)
3917 {
3918 add_host_route_array(rb, &pai->DnsServerList);
3919 }
3920 }
3921
3922 gc_free(&gc);
3923}
3924
3925#else /* if defined(_WIN32) */
3926
3927static void
3928// cppcheck-suppress constParameterPointer ; stub
3929get_bypass_addresses(struct route_bypass *rb, const unsigned int flags)
3930{
3931}
3932
3933#endif /* if defined(_WIN32) */
3934
3935/*
3936 * Test if addr is reachable via a local interface (return ILA_LOCAL),
3937 * or if it needs to be routed via the default gateway (return
3938 * ILA_NONLOCAL). If the target platform doesn't implement this
3939 * function, return ILA_NOT_IMPLEMENTED.
3940 *
3941 * Used by redirect-gateway autolocal feature
3942 */
3943
3944#if defined(_WIN32)
3945
3946int
3947test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi)
3948{
3949 struct gc_arena gc = gc_new();
3950 const in_addr_t nonlocal_netmask =
3951 0x80000000L; /* routes with netmask <= to this are considered non-local */
3952 int ret = TLA_NONLOCAL;
3953
3954 /* get full routing table */
3955 const MIB_IPFORWARDTABLE *rt = get_windows_routing_table(&gc);
3956 if (rt)
3957 {
3958 for (DWORD i = 0; i < rt->dwNumEntries; ++i)
3959 {
3960 const MIB_IPFORWARDROW *row = &rt->table[i];
3961 const in_addr_t net = ntohl(row->dwForwardDest);
3962 const in_addr_t mask = ntohl(row->dwForwardMask);
3963 if (mask > nonlocal_netmask && (addr & mask) == net)
3964 {
3965 ret = TLA_LOCAL;
3966 break;
3967 }
3968 }
3969 }
3970
3971 gc_free(&gc);
3972 return ret;
3973}
3974
3975#else /* if defined(_WIN32) */
3976
3977int
3978test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi) /* PLATFORM-SPECIFIC */
3979{
3980 if (rgi)
3981 {
3982 if (local_route(addr, 0xFFFFFFFF, rgi->gateway.addr, rgi))
3983 {
3984 return TLA_LOCAL;
3985 }
3986 else
3987 {
3988 return TLA_NONLOCAL;
3989 }
3990 }
3991 return TLA_NOT_IMPLEMENTED;
3992}
3993
3994#endif /* if defined(_WIN32) */
void argv_msg(const msglvl_t msglevel, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
Definition argv.c:242
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:438
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:462
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
char * format_hex_ex(const uint8_t *data, size_t size, size_t maxoutput, unsigned int space_break_flags, const char *separator, struct gc_arena *gc)
Format a binary buffer as a hex string.
Definition buffer.c:460
void gc_addspecial(void *addr, void(*free_function)(void *), struct gc_arena *a)
Register an address with a custom free function in a garbage collection arena.
Definition buffer.c:414
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2132
#define ALLOC_OBJ_GC(dptr, type, gc)
Allocate a garbage-collected object of the given type (uninitialised).
Definition buffer.h:2120
static void check_malloc_return(void *p)
Abort if a memory allocation returned NULL.
Definition buffer.h:2144
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static void gc_freeaddrinfo_callback(void *addr)
Callback to free a struct addrinfo, suitable for use with gc_addspecial().
Definition buffer.h:377
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
size_t strlcpy(char *dest, const char *src, size_t maxlen)
void setenv_int_i(struct env_set *es, const char *name, const int value, const int i)
Definition env_set.c:412
void setenv_str_i(struct env_set *es, const char *name, const char *value, const int i)
Definition env_set.c:421
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:305
#define D_ROUTE_DEBUG
Definition errlevel.h:129
#define M_INFO
Definition errlevel.h:54
#define D_ROUTE
Definition errlevel.h:78
static SERVICE_STATUS status
Definition interactive.c:52
@ route
Definition interactive.c:83
@ write
@ read
void management_set_state(struct management *man, const int state, const char *detail, const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, const struct openvpn_sockaddr *local, const struct openvpn_sockaddr *remote)
Definition manage.c:2915
#define OPENVPN_STATE_ADD_ROUTES
Definition manage.h:462
#define NET_ROUTE_TABLE_DEFAULT
net_route_*() table: Linux main table, FreeBSD FIB of the process
Definition networking.h:28
static void net_ctx_reset(openvpn_net_ctx_t *ctx)
Definition networking.h:59
void * openvpn_net_ctx_t
Definition networking.h:41
#define TUN_ADAPTER_INDEX_INVALID
Definition openvpn-msg.h:69
@ msg_add_route
Definition openvpn-msg.h:34
@ msg_del_route
Definition openvpn-msg.h:35
#define IPV4_NETMASK_HOST
Definition basic.h:34
#define CLEAR(x)
Definition basic.h:32
const char * strerror_win32(DWORD errnum, struct gc_arena *gc)
Definition error.c:786
#define M_FATAL
Definition error.h:90
#define dmsg(flags,...)
Definition error.h:164
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
#define DEV_TYPE_TAP
Definition proto.h:36
#define DEV_TYPE_TUN
Definition proto.h:35
int openvpn_getaddrinfo(unsigned int flags, const char *hostname, const char *servname, int resolve_retry_seconds, struct signal_info *sig_info, int ai_family, struct addrinfo **res)
Definition resolve.c:102
bool get_ipv6_addr(const char *hostname, struct in6_addr *network, unsigned int *netbits, msglvl_t msglevel)
Translate an IPv6 addr or hostname from string form to in6_addr.
Definition resolve.c:489
in_addr_t getaddr(unsigned int flags, const char *hostname, int resolve_retry_seconds, bool *succeeded, struct signal_info *sig_info)
Translate an IPv4 addr or hostname from string form to in_addr_t.
Definition resolve.c:442
DNS resolution.
#define GETADDR_HOST_ORDER
Definition resolve.h:40
#define GETADDR_RESOLVE
Definition resolve.h:38
#define GETADDR_WARN_ON_SIGNAL
Definition resolve.h:43
void print_route_options(const struct route_option_list *rol, msglvl_t msglevel)
Definition route.c:1239
static void undo_redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1055
static void print_route_option(const struct route_option *ro, msglvl_t msglevel)
Definition route.c:1232
static void add_host_route_if_nonlocal(struct route_bypass *rb, const in_addr_t addr)
Definition route.c:3865
bool add_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1102
bool is_special_addr(const char *addr_str)
Definition route.c:289
struct route_option_list * clone_route_option_list(const struct route_option_list *src, struct gc_arena *a)
Definition route.c:156
void setenv_routes_ipv6(struct env_set *es, const struct route_ipv6_list *rl6)
Definition route.c:1373
static void clear_route_list(struct route_list *rl)
Definition route.c:511
static void test_route_helper(bool *ret, int *count, int *good, int *ambig, const IP_ADAPTER_INFO *adapters, const in_addr_t gateway)
Definition route.c:2366
static bool del_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt)
Definition route.c:2757
static bool redirect_default_route_to_vpn(struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:947
static bool add_route3(in_addr_t network, in_addr_t netmask, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:881
static void add_host_route_array(struct route_bypass *rb, const IP_ADDR_STRING *iplist)
Definition route.c:3874
int netmask_to_netbits2(in_addr_t netmask)
Definition route.c:3839
struct route_ipv6_option_list * new_route_ipv6_option_list(struct gc_arena *a)
Definition route.c:140
static bool init_route(struct route_ipv4 *r, struct addrinfo **network_list, const struct route_option *ro, const struct route_list *rl)
Definition route.c:302
void delete_routes_v6(struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1197
static bool get_special_addr(const struct route_list *rl, const char *string, in_addr_t *out, bool *status)
Definition route.c:221
#define RTA_EEXIST
Definition route.c:104
#define LR_NOMATCH
Definition route.c:1402
bool test_routes(const struct route_list *rl, const struct tuntap *tt)
Definition route.c:2391
static void add_block_local_routes(struct route_list *rl)
Definition route.c:563
bool block_local_needed(const struct route_list *rl)
Get the decision whether to block traffic to local networks while the VPN is connected.
Definition route.c:592
void get_default_gateway_ipv6(struct route_ipv6_gateway_info *rgi6, const struct in6_addr *dest, openvpn_net_ctx_t *ctx)
Definition route.c:2615
static const MIB_IPFORWARDTABLE * get_windows_routing_table(struct gc_arena *gc)
Definition route.c:2332
static DWORD get_best_route(struct gc_arena *gc, SOCKADDR_INET *dest, MIB_IPFORWARD_ROW2 *best_route)
Determines the best route to a destination for both IPv4 and IPv6.
Definition route.c:2482
static int do_route_service(const bool add, const route_message_t *rt, const DWORD size, HANDLE pipe)
Definition route.c:2792
static const char * format_route_entry(const MIB_IPFORWARDROW *r, struct gc_arena *gc)
Definition route.c:2997
static int add_route_service(const struct route_ipv4 *, const struct tuntap *)
Definition route.c:2972
bool init_route_ipv6_list(struct route_ipv6_list *rl6, const struct route_ipv6_option_list *opt6, const char *remote_endpoint, int default_metric, const struct in6_addr *remote_host_ipv6, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:746
static void delete_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1986
static bool del_route_ipv6_service(const struct route_ipv6 *, const struct tuntap *)
Definition route.c:2991
void route_list_add_vpn_gateway(struct route_list *rl, struct env_set *es, const in_addr_t addr)
Definition route.c:525
bool ipv6_net_contains_host(const struct in6_addr *network, unsigned int bits, const struct in6_addr *host)
check whether an IPv6 host address is covered by a given network/bits
Definition route.c:712
void add_route_ipv6_to_option_list(struct route_ipv6_option_list *l, const char *prefix, const char *gateway, const char *metric, int table_id)
Definition route.c:497
void copy_route_option_list(struct route_option_list *dest, const struct route_option_list *src, struct gc_arena *a)
Definition route.c:174
void copy_route_ipv6_option_list(struct route_ipv6_option_list *dest, const struct route_ipv6_option_list *src, struct gc_arena *a)
Definition route.c:182
bool add_route(struct route_ipv4 *r, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1448
static int test_route(const IP_ADAPTER_INFO *adapters, const in_addr_t gateway, DWORD *index)
Definition route.c:2354
void print_default_gateway(const msglvl_t msglevel, const struct route_gateway_info *rgi, const struct route_ipv6_gateway_info *rgi6)
Definition route.c:1253
bool add_route_ipv6(struct route_ipv6 *r6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1750
#define RTA_ERROR
Definition route.c:102
static bool del_route_service(const struct route_ipv4 *, const struct tuntap *)
Definition route.c:2978
static int local_route(in_addr_t network, in_addr_t netmask, in_addr_t gateway, const struct route_gateway_info *rgi)
Definition route.c:1406
static int add_route_ipv6_service(const struct route_ipv6 *, const struct tuntap *)
Definition route.c:2985
static const MIB_IPFORWARDROW * get_default_gateway_row(const MIB_IPFORWARDTABLE *routes)
Definition route.c:2434
void get_default_gateway(struct route_gateway_info *rgi, in_addr_t dest, openvpn_net_ctx_t *ctx)
Retrieves the best gateway for a given destination based on the routing table.
Definition route.c:2518
static int route_ipv6_ipapi(bool add, const struct route_ipv6 *, const struct tuntap *)
Definition route.c:2853
static void add_block_local_item(struct route_list *rl, const struct route_gateway_address *gateway, in_addr_t target)
Definition route.c:534
struct route_ipv6_option_list * clone_route_ipv6_option_list(const struct route_ipv6_option_list *src, struct gc_arena *a)
Definition route.c:165
static const char * show_opt(const char *option)
Definition route.c:1219
static bool init_route_ipv6(struct route_ipv6 *r6, const struct route_ipv6_option *r6o, const struct route_ipv6_list *rl6)
Definition route.c:425
struct route_option_list * new_route_option_list(struct gc_arena *a)
Definition route.c:131
int test_local_addr(const in_addr_t addr, const struct route_gateway_info *rgi)
Definition route.c:3947
#define METRIC_NOT_USED
Definition route.c:60
bool init_route_list(struct route_list *rl, const struct route_option_list *opt, const char *remote_endpoint, int default_metric, in_addr_t remote_host, struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:600
static int add_route_ipapi(const struct route_ipv4 *r, const struct tuntap *tt, DWORD adapter_index)
Definition route.c:2661
static bool add_bypass_routes(const struct route_bypass *rb, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:911
#define RTA_SUCCESS
Definition route.c:103
void delete_route_ipv6(const struct route_ipv6 *r6, const struct tuntap *tt, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:2151
#define LR_MATCH
Definition route.c:1403
static int do_route_ipv6_service(const bool add, const struct route_ipv6 *r, const struct tuntap *tt)
Definition route.c:2930
static bool is_on_link(const struct route_ipv4 *r, const unsigned int flags, const struct route_gateway_info *rgi)
Definition route.c:1438
static void del_bypass_routes(const struct route_bypass *rb, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:930
void route_ipv6_clear_host_bits(struct route_ipv6 *r6)
Definition route.c:1725
static void clear_route_ipv6_list(struct route_ipv6_list *rl6)
Definition route.c:518
void delete_routes(struct route_list *rl, struct route_ipv6_list *rl6, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1167
void show_routes(msglvl_t msglevel)
Definition route.c:3015
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:3809
static void setenv_route_addr(struct env_set *es, const char *key, const in_addr_t addr, int i)
Definition route.c:204
static DWORD windows_route_find_if_index(const struct route_ipv4 *r, const struct tuntap *tt)
Definition route.c:2570
static int do_route_ipv4_service(const bool add, const struct route_ipv4 *r, const struct tuntap *tt)
Definition route.c:2824
void setenv_routes(struct env_set *es, const struct route_list *rl)
Definition route.c:1343
void delete_routes_v4(struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:1175
static void get_bypass_addresses(struct route_bypass *rb, const unsigned int flags)
Definition route.c:3890
static bool is_route_parm_defined(const char *parm)
Definition route.c:190
static void setenv_route_ipv6(struct env_set *es, const struct route_ipv6 *r6, int i)
Definition route.c:1354
static bool add_bypass_address(struct route_bypass *rb, const in_addr_t a)
Definition route.c:108
static void print_bypass_addresses(const struct route_bypass *rb)
Definition route.c:88
static void setenv_route(struct env_set *es, const struct route_ipv4 *r, int i)
Definition route.c:1327
void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask, const char *gateway, const char *metric, int table_id)
Definition route.c:482
static void del_route3(in_addr_t network, in_addr_t netmask, in_addr_t gateway, const struct tuntap *tt, unsigned int flags, const struct route_gateway_info *rgi, const struct env_set *es, openvpn_net_ctx_t *ctx)
Definition route.c:896
#define RG_LOCAL
Definition route.h:87
#define TLA_LOCAL
Definition route.h:372
#define RGI_ON_LINK
Definition route.h:164
#define RL_DID_LOCAL
Definition route.h:231
#define RL_DID_REDIRECT_DEFAULT_GATEWAY
Definition route.h:230
#define ROUTE_REF_GW
Definition route.h:50
#define RG_BYPASS_DHCP
Definition route.h:89
#define RTSA_REMOTE_ENDPOINT
Definition route.h:62
#define RGI_ADDR_DEFINED
Definition route.h:159
#define RL_ROUTES_ADDED
Definition route.h:232
#define RGI_HWADDR_DEFINED
Definition route.h:161
static in_addr_t netbits_to_netmask(const int netbits)
Definition route.h:399
#define RTSA_REMOTE_HOST
Definition route.h:63
#define ROUTE_METHOD_SERVICE
Definition route.h:42
#define RGI_IFACE_DEFINED
Definition route.h:162
#define ROUTE_METHOD_IPAPI
Definition route.h:40
#define RT_ADDED
Definition route.h:121
#define ROUTE_METHOD_EXE
Definition route.h:41
#define RT_METRIC_DEFINED
Definition route.h:122
#define ROUTE_DELETE_FIRST
Definition route.h:49
#define RG_DEF1
Definition route.h:88
#define RG_BYPASS_DNS
Definition route.h:90
#define RG_ENABLE
Definition route.h:86
#define RG_REROUTE_GW
Definition route.h:91
#define TLA_NOT_IMPLEMENTED
Definition route.h:370
#define ROUTE_METHOD_ADAPTIVE
Definition route.h:39
#define RG_AUTO_LOCAL
Definition route.h:92
#define RG_BLOCK_LOCAL
Definition route.h:93
#define TLA_NONLOCAL
Definition route.h:371
#define N_ROUTE_BYPASS
Definition route.h:54
#define RT_DEFINED
Definition route.h:120
#define ROUTE_METHOD_MASK
Definition route.h:43
#define RTSA_DEFAULT_METRIC
Definition route.h:64
#define RGI_NETMASK_DEFINED
Definition route.h:160
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
#define IPV4_INVALID_ADDR
Definition socket.h:381
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
#define IF_NAMESIZE
#define IA_NET_ORDER
Definition socket_util.h:90
Definition argv.h:35
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:145
int n_bypass
Definition route.h:55
in_addr_t bypass[N_ROUTE_BYPASS]
Definition route.h:56
in_addr_t netmask
Definition route.h:154
uint8_t hwaddr[6]
Definition route.h:177
unsigned int flags
Definition route.h:165
struct route_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:185
DWORD adapter_index
Definition route.h:169
struct route_gateway_address gateway
Definition route.h:180
const struct route_option * option
Definition route.h:125
int metric
Definition route.h:130
struct route_ipv4 * next
Definition route.h:123
int table_id
Definition route.h:129
in_addr_t network
Definition route.h:126
in_addr_t netmask
Definition route.h:127
in_addr_t gateway
Definition route.h:128
unsigned int flags
Definition route.h:124
struct in6_addr addr_ipv6
Definition route.h:190
struct route_ipv6_gateway_address gateway
Definition route.h:219
struct route_ipv6_gateway_address addrs[RGI_N_ADDRESSES]
Definition route.h:224
unsigned int flags
Definition route.h:197
uint8_t hwaddr[6]
Definition route.h:216
unsigned int iflags
Definition route.h:245
unsigned int flags
Definition route.h:254
struct route_ipv6_gateway_info rgi6
Definition route.h:252
struct route_ipv6 * routes_ipv6
Definition route.h:255
unsigned int spec_flags
Definition route.h:247
struct route_ipv6_gateway_info ngi6
Definition route.h:253
int default_metric
Definition route.h:250
struct in6_addr remote_host_ipv6
Definition route.h:249
struct gc_arena gc
Definition route.h:256
struct in6_addr remote_endpoint_ipv6
Definition route.h:248
unsigned int flags
Definition route.h:113
struct gc_arena * gc
Definition route.h:115
struct route_ipv6_option * routes_ipv6
Definition route.h:114
struct route_ipv6_option * next
Definition route.h:104
const char * gateway
Definition route.h:106
const char * metric
Definition route.h:107
const char * prefix
Definition route.h:105
struct in6_addr gateway
Definition route.h:139
DWORD adapter_index
Definition route.h:144
unsigned int netbits
Definition route.h:138
struct route_ipv6 * next
Definition route.h:135
unsigned int flags
Definition route.h:136
struct in6_addr network
Definition route.h:137
int metric
Definition route.h:140
struct route_gateway_info rgi
Definition route.h:236
struct route_ipv4 * routes
Definition route.h:239
struct route_special_addr spec
Definition route.h:235
unsigned int flags
Definition route.h:238
struct gc_arena gc
Definition route.h:240
struct route_gateway_info ngi
Definition route.h:237
unsigned int iflags
Definition route.h:233
interface_t iface
Definition openvpn-msg.h:96
struct gc_arena * gc
Definition route.h:99
unsigned int flags
Definition route.h:97
struct route_option * routes
Definition route.h:98
int table_id
Definition route.h:81
const char * network
Definition route.h:78
const char * netmask
Definition route.h:79
struct route_option * next
Definition route.h:77
const char * gateway
Definition route.h:80
const char * metric
Definition route.h:82
in_addr_t remote_host
Definition route.h:68
struct route_bypass bypass
Definition route.h:70
unsigned int flags
Definition route.h:65
in_addr_t remote_endpoint
Definition route.h:67
int remote_host_local
Definition route.h:69
HANDLE msg_channel
Definition tun.h:86
Definition tun.h:181
int type
Definition tun.h:183
DWORD adapter_index
Definition tun.h:232
bool did_ifconfig_ipv6_setup
if the internal variables related to ifconfig-ipv6 of this struct have been set up.
Definition tun.h:199
struct tuntap_options options
Definition tun.h:203
bool did_ifconfig_setup
if the internal variables related to ifconfig of this struct have been set up.
Definition tun.h:195
char * actual_name
Definition tun.h:205
uint32_t in_addr_t
Definition syshead.h:52
static char * iface
char * r6[]
char * r1[]
struct gc_arena gc
Definition test_ssl.c:138
const IP_ADAPTER_INFO * get_tun_adapter(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
Definition tun.c:4411
bool is_adapter_up(const struct tuntap *tt, const IP_ADAPTER_INFO *list)
Definition tun.c:4424
const IP_ADAPTER_INFO * get_adapter_info(DWORD index, struct gc_arena *gc)
Definition tun.c:4326
const IP_PER_ADAPTER_INFO * get_per_adapter_info(const DWORD index, struct gc_arena *gc)
Definition tun.c:4221
bool is_ip_in_adapter_subnet(const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask)
Definition tun.c:4467
const IP_ADAPTER_INFO * get_adapter_info_list(struct gc_arena *gc)
Definition tun.c:4196
const IP_ADAPTER_INFO * get_adapter(const IP_ADAPTER_INFO *ai, DWORD index)
Definition tun.c:4307
DWORD adapter_index_of_ip(const IP_ADAPTER_INFO *list, const in_addr_t ip, int *count, in_addr_t *netmask)
Definition tun.c:4500
WCHAR * wide_string(const char *utf8, struct gc_arena *gc)
Definition win32-util.c:45
void netcmd_semaphore_release(void)
Definition win32.c:971
char * get_win_sys_path(void)
Definition win32.c:1162
void netcmd_semaphore_lock(void)
Definition win32.c:955
bool send_msg_iservice(HANDLE pipe, const void *data, DWORD size, ack_message_t *ack, const char *context)
Send the size bytes in buffer data to the interactive service pipe and read the result in ack.
Definition win32.c:1457
#define WIN_ROUTE_PATH_SUFFIX
Definition win32.h:40