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