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