big svn cleanup
[anytun.git] / src / openvpn / route.c
blobcbcbf123c9289a1280a54c6a8be9f3dec65e3913
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.
8 * Copyright (C) 2002-2005 OpenVPN Solutions LLC <info@openvpn.net>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Support routines for adding/deleting network routes.
29 #ifdef WIN32
30 #include "config-win32.h"
31 #else
32 #include "config.h"
33 #endif
35 #include "syshead.h"
37 #include "common.h"
38 #include "error.h"
39 #include "route.h"
40 #include "misc.h"
41 #include "socket.h"
42 #include "manage.h"
44 #include "memdbg.h"
46 static void add_route (struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
47 static void delete_route (const struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es);
48 static bool get_default_gateway (in_addr_t *ret);
50 struct route_option_list *
51 new_route_option_list (struct gc_arena *a)
53 struct route_option_list *ret;
54 ALLOC_OBJ_CLEAR_GC (ret, struct route_option_list, a);
55 return ret;
58 struct route_list *
59 new_route_list (struct gc_arena *a)
61 struct route_list *ret;
62 ALLOC_OBJ_CLEAR_GC (ret, struct route_list, a);
63 return ret;
66 static const char *
67 route_string (const struct route *r, struct gc_arena *gc)
69 struct buffer out = alloc_buf_gc (256, gc);
70 buf_printf (&out, "ROUTE network %s netmask %s gateway %s",
71 print_in_addr_t (r->network, 0, gc),
72 print_in_addr_t (r->netmask, 0, gc),
73 print_in_addr_t (r->gateway, 0, gc)
75 if (r->metric_defined)
76 buf_printf (&out, " metric %d", r->metric);
77 return BSTR (&out);
80 static bool
81 is_route_parm_defined (const char *parm)
83 if (!parm)
84 return false;
85 if (!strcmp (parm, "default"))
86 return false;
87 return true;
90 static void
91 setenv_route_addr (struct env_set *es, const char *key, const in_addr_t addr, int i)
93 struct gc_arena gc = gc_new ();
94 struct buffer name = alloc_buf_gc (256, &gc);
95 if (i >= 0)
96 buf_printf (&name, "route_%s_%d", key, i);
97 else
98 buf_printf (&name, "route_%s", key);
99 setenv_str (es, BSTR (&name), print_in_addr_t (addr, 0, &gc));
100 gc_free (&gc);
103 static bool
104 get_special_addr (const struct route_special_addr *spec,
105 const char *string,
106 in_addr_t *out,
107 bool *status)
109 *status = true;
110 if (!strcmp (string, "vpn_gateway"))
112 if (spec->remote_endpoint_defined)
113 *out = spec->remote_endpoint;
114 else
116 msg (M_INFO, PACKAGE_NAME " ROUTE: vpn_gateway undefined");
117 *status = false;
119 return true;
121 else if (!strcmp (string, "net_gateway"))
123 if (spec->net_gateway_defined)
124 *out = spec->net_gateway;
125 else
127 msg (M_INFO, PACKAGE_NAME " ROUTE: net_gateway undefined -- unable to get default gateway from system");
128 *status = false;
130 return true;
132 else if (!strcmp (string, "remote_host"))
134 if (spec->remote_host_defined)
135 *out = spec->remote_host;
136 else
138 msg (M_INFO, PACKAGE_NAME " ROUTE: remote_host undefined");
139 *status = false;
141 return true;
143 return false;
146 static bool
147 init_route (struct route *r,
148 const struct route_option *ro,
149 const struct route_special_addr *spec)
151 const in_addr_t default_netmask = ~0;
152 bool status;
154 r->option = ro;
155 r->defined = false;
157 /* network */
159 if (!is_route_parm_defined (ro->network))
161 goto fail;
164 if (!get_special_addr (spec, ro->network, &r->network, &status))
166 r->network = getaddr (
167 GETADDR_RESOLVE
168 | GETADDR_HOST_ORDER
169 | GETADDR_WARN_ON_SIGNAL,
170 ro->network,
172 &status,
173 NULL);
176 if (!status)
177 goto fail;
179 /* netmask */
181 if (is_route_parm_defined (ro->netmask))
183 r->netmask = getaddr (
184 GETADDR_HOST_ORDER
185 | GETADDR_WARN_ON_SIGNAL,
186 ro->netmask,
188 &status,
189 NULL);
190 if (!status)
191 goto fail;
193 else
194 r->netmask = default_netmask;
196 /* gateway */
198 if (is_route_parm_defined (ro->gateway))
200 if (!get_special_addr (spec, ro->gateway, &r->gateway, &status))
202 r->gateway = getaddr (
203 GETADDR_RESOLVE
204 | GETADDR_HOST_ORDER
205 | GETADDR_WARN_ON_SIGNAL,
206 ro->gateway,
208 &status,
209 NULL);
211 if (!status)
212 goto fail;
214 else
216 if (spec->remote_endpoint_defined)
217 r->gateway = spec->remote_endpoint;
218 else
220 msg (M_WARN, PACKAGE_NAME " ROUTE: " PACKAGE_NAME " needs a gateway parameter for a --route option and no default was specified by either --route-gateway or --ifconfig options");
221 goto fail;
225 /* metric */
227 r->metric_defined = false;
228 r->metric = 0;
229 if (is_route_parm_defined (ro->metric))
231 r->metric = atoi (ro->metric);
232 if (r->metric < 0)
234 msg (M_WARN, PACKAGE_NAME " ROUTE: route metric for network %s (%s) must be >= 0",
235 ro->network,
236 ro->metric);
237 goto fail;
239 r->metric_defined = true;
241 else
243 r->metric = 0;
244 r->metric_defined = false;
247 r->defined = true;
249 return true;
251 fail:
252 msg (M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve route for host/network: %s",
253 ro->network);
254 r->defined = false;
255 return false;
258 void
259 add_route_to_option_list (struct route_option_list *l,
260 const char *network,
261 const char *netmask,
262 const char *gateway,
263 const char *metric)
265 struct route_option *ro;
266 if (l->n >= MAX_ROUTES)
267 msg (M_FATAL, PACKAGE_NAME " ROUTE: cannot add more than %d routes",
268 MAX_ROUTES);
269 ro = &l->routes[l->n];
270 ro->network = network;
271 ro->netmask = netmask;
272 ro->gateway = gateway;
273 ro->metric = metric;
274 ++l->n;
277 void
278 clear_route_list (struct route_list *rl)
280 CLEAR (*rl);
283 bool
284 init_route_list (struct route_list *rl,
285 const struct route_option_list *opt,
286 const char *remote_endpoint,
287 in_addr_t remote_host,
288 struct env_set *es)
290 struct gc_arena gc = gc_new ();
291 bool ret = true;
293 clear_route_list (rl);
295 if (remote_host)
297 rl->spec.remote_host = remote_host;
298 rl->spec.remote_host_defined = true;
301 rl->spec.net_gateway_defined = get_default_gateway (&rl->spec.net_gateway);
302 if (rl->spec.net_gateway_defined)
304 setenv_route_addr (es, "net_gateway", rl->spec.net_gateway, -1);
305 dmsg (D_ROUTE_DEBUG, "ROUTE: default_gateway=%s", print_in_addr_t (rl->spec.net_gateway, 0, &gc));
307 else
309 dmsg (D_ROUTE_DEBUG, "ROUTE: default_gateway=UNDEF");
311 rl->redirect_default_gateway = opt->redirect_default_gateway;
312 rl->redirect_local = opt->redirect_local;
313 rl->redirect_def1 = opt->redirect_def1;
315 if (is_route_parm_defined (remote_endpoint))
317 rl->spec.remote_endpoint = getaddr (
318 GETADDR_RESOLVE
319 | GETADDR_HOST_ORDER
320 | GETADDR_WARN_ON_SIGNAL,
321 remote_endpoint,
323 &rl->spec.remote_endpoint_defined,
324 NULL);
326 if (rl->spec.remote_endpoint_defined)
328 setenv_route_addr (es, "vpn_gateway", rl->spec.remote_endpoint, -1);
330 else
332 msg (M_WARN, PACKAGE_NAME " ROUTE: failed to parse/resolve default gateway: %s",
333 remote_endpoint);
334 ret = false;
337 else
338 rl->spec.remote_endpoint_defined = false;
340 ASSERT (opt->n >= 0 && opt->n < MAX_ROUTES);
342 /* parse the routes from opt to rl */
344 int i, j = 0;
345 for (i = 0; i < opt->n; ++i)
347 if (!init_route (&rl->routes[j],
348 &opt->routes[i],
349 &rl->spec))
350 ret = false;
351 else
352 ++j;
354 rl->n = j;
357 gc_free (&gc);
358 return ret;
361 static void
362 add_route3 (in_addr_t network,
363 in_addr_t netmask,
364 in_addr_t gateway,
365 const struct tuntap *tt,
366 unsigned int flags,
367 const struct env_set *es)
369 struct route r;
370 CLEAR (r);
371 r.defined = true;
372 r.network = network;
373 r.netmask = netmask;
374 r.gateway = gateway;
375 add_route (&r, tt, flags, es);
378 static void
379 del_route3 (in_addr_t network,
380 in_addr_t netmask,
381 in_addr_t gateway,
382 const struct tuntap *tt,
383 unsigned int flags,
384 const struct env_set *es)
386 struct route r;
387 CLEAR (r);
388 r.defined = true;
389 r.network = network;
390 r.netmask = netmask;
391 r.gateway = gateway;
392 delete_route (&r, tt, flags, es);
395 static void
396 redirect_default_route_to_vpn (struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
398 const char err[] = "NOTE: unable to redirect default gateway --";
400 if (rl->redirect_default_gateway)
402 if (!rl->spec.remote_endpoint_defined)
404 msg (M_WARN, "%s VPN gateway parameter (--route-gateway or --ifconfig) is missing", err);
406 else if (!rl->spec.net_gateway_defined)
408 msg (M_WARN, "%s Cannot read current default gateway from system", err);
410 else if (!rl->spec.remote_host_defined)
412 msg (M_WARN, "%s Cannot obtain current remote host address", err);
414 else
416 /* route remote host to original default gateway */
417 if (!rl->redirect_local)
418 add_route3 (rl->spec.remote_host,
420 rl->spec.net_gateway,
422 flags,
423 es);
425 if (rl->redirect_def1)
427 /* add new default route (1st component) */
428 add_route3 (0x00000000,
429 0x80000000,
430 rl->spec.remote_endpoint,
432 flags,
433 es);
435 /* add new default route (2nd component) */
436 add_route3 (0x80000000,
437 0x80000000,
438 rl->spec.remote_endpoint,
440 flags,
441 es);
443 else
445 /* delete default route */
446 del_route3 (0,
448 rl->spec.net_gateway,
450 flags,
451 es);
453 /* add new default route */
454 add_route3 (0,
456 rl->spec.remote_endpoint,
458 flags,
459 es);
462 /* set a flag so we can undo later */
463 rl->did_redirect_default_gateway = true;
468 static void
469 undo_redirect_default_route_to_vpn (struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
471 if (rl->did_redirect_default_gateway)
473 /* delete remote host route */
474 if (!rl->redirect_local)
475 del_route3 (rl->spec.remote_host,
477 rl->spec.net_gateway,
479 flags,
480 es);
482 if (rl->redirect_def1)
484 /* delete default route (1st component) */
485 del_route3 (0x00000000,
486 0x80000000,
487 rl->spec.remote_endpoint,
489 flags,
490 es);
492 /* delete default route (2nd component) */
493 del_route3 (0x80000000,
494 0x80000000,
495 rl->spec.remote_endpoint,
497 flags,
498 es);
500 else
502 /* delete default route */
503 del_route3 (0,
505 rl->spec.remote_endpoint,
507 flags,
508 es);
510 /* restore original default route */
511 add_route3 (0,
513 rl->spec.net_gateway,
515 flags,
516 es);
519 rl->did_redirect_default_gateway = false;
523 void
524 add_routes (struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
526 redirect_default_route_to_vpn (rl, tt, flags, es);
527 if (!rl->routes_added)
529 int i;
531 #ifdef ENABLE_MANAGEMENT
532 if (management && rl->n)
534 management_set_state (management,
535 OPENVPN_STATE_ADD_ROUTES,
536 NULL,
539 #endif
541 for (i = 0; i < rl->n; ++i)
543 if (flags & ROUTE_DELETE_FIRST)
544 delete_route (&rl->routes[i], tt, flags, es);
545 add_route (&rl->routes[i], tt, flags, es);
547 rl->routes_added = true;
551 void
552 delete_routes (struct route_list *rl, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
554 if (rl->routes_added)
556 int i;
557 for (i = rl->n - 1; i >= 0; --i)
559 const struct route *r = &rl->routes[i];
560 delete_route (r, tt, flags, es);
562 rl->routes_added = false;
564 undo_redirect_default_route_to_vpn (rl, tt, flags, es);
566 CLEAR (*rl);
569 #ifdef ENABLE_DEBUG
571 static const char *
572 show_opt (const char *option)
574 if (!option)
575 return "nil";
576 else
577 return option;
580 static void
581 print_route_option (const struct route_option *ro, int level)
583 msg (level, " route %s/%s/%s/%s",
584 show_opt (ro->network),
585 show_opt (ro->netmask),
586 show_opt (ro->gateway),
587 show_opt (ro->metric));
590 void
591 print_route_options (const struct route_option_list *rol,
592 int level)
594 int i;
595 if (rol->redirect_default_gateway)
596 msg (level, " [redirect_default_gateway local=%d]",
597 rol->redirect_local);
598 for (i = 0; i < rol->n; ++i)
599 print_route_option (&rol->routes[i], level);
602 #endif
604 static void
605 print_route (const struct route *r, int level)
607 struct gc_arena gc = gc_new ();
608 if (r->defined)
609 msg (level, "%s", route_string (r, &gc));
610 gc_free (&gc);
613 void
614 print_routes (const struct route_list *rl, int level)
616 int i;
617 for (i = 0; i < rl->n; ++i)
618 print_route (&rl->routes[i], level);
621 static void
622 setenv_route (struct env_set *es, const struct route *r, int i)
624 struct gc_arena gc = gc_new ();
625 if (r->defined)
627 setenv_route_addr (es, "network", r->network, i);
628 setenv_route_addr (es, "netmask", r->netmask, i);
629 setenv_route_addr (es, "gateway", r->gateway, i);
631 if (r->metric_defined)
633 struct buffer name = alloc_buf_gc (256, &gc);
634 buf_printf (&name, "route_metric_%d", i);
635 setenv_int (es, BSTR (&name), r->metric);
638 gc_free (&gc);
641 void
642 setenv_routes (struct env_set *es, const struct route_list *rl)
644 int i;
645 for (i = 0; i < rl->n; ++i)
646 setenv_route (es, &rl->routes[i], i + 1);
649 static void
650 add_route (struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
652 struct gc_arena gc;
653 struct buffer buf;
654 const char *network;
655 const char *netmask;
656 const char *gateway;
657 bool status = false;
659 if (!r->defined)
660 return;
662 gc_init (&gc);
663 buf = alloc_buf_gc (256, &gc);
665 network = print_in_addr_t (r->network, 0, &gc);
666 netmask = print_in_addr_t (r->netmask, 0, &gc);
667 gateway = print_in_addr_t (r->gateway, 0, &gc);
670 * Filter out routes which are essentially no-ops
672 if (r->network == r->gateway && r->netmask == 0xFFFFFFFF)
674 msg (M_INFO, PACKAGE_NAME " ROUTE: omitted no-op route: %s/%s -> %s",
675 network, netmask, gateway);
676 goto done;
679 #if defined(TARGET_LINUX)
680 #ifdef CONFIG_FEATURE_IPROUTE
681 buf_printf (&buf, IPROUTE_PATH " route add %s/%d via %s",
682 network,
683 count_netmask_bits(netmask),
684 gateway);
685 if (r->metric_defined)
686 buf_printf (&buf, " metric %d", r->metric);
688 #else
689 buf_printf (&buf, ROUTE_PATH " add -net %s netmask %s gw %s",
690 network,
691 netmask,
692 gateway);
693 if (r->metric_defined)
694 buf_printf (&buf, " metric %d", r->metric);
695 #endif /*CONFIG_FEATURE_IPROUTE*/
696 msg (D_ROUTE, "%s", BSTR (&buf));
697 status = system_check (BSTR (&buf), es, 0, "ERROR: Linux route add command failed");
699 #elif defined (WIN32)
701 buf_printf (&buf, ROUTE_PATH " ADD %s MASK %s %s",
702 network,
703 netmask,
704 gateway);
705 if (r->metric_defined)
706 buf_printf (&buf, " METRIC %d", r->metric);
708 msg (D_ROUTE, "%s", BSTR (&buf));
710 if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_IPAPI)
712 status = add_route_ipapi (r, tt);
713 msg (D_ROUTE, "Route addition via IPAPI %s", status ? "succeeded" : "failed");
715 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_EXE)
717 netcmd_semaphore_lock ();
718 status = system_check (BSTR (&buf), es, 0, "ERROR: Windows route add command failed");
719 netcmd_semaphore_release ();
721 else
723 ASSERT (0);
726 #elif defined (TARGET_SOLARIS)
728 /* example: route add 192.0.2.32 -netmask 255.255.255.224 somegateway */
730 buf_printf (&buf, ROUTE_PATH " add");
732 #if 0
733 if (r->metric_defined)
734 buf_printf (&buf, " -rtt %d", r->metric);
735 #endif
737 buf_printf (&buf, " %s -netmask %s %s",
738 network,
739 netmask,
740 gateway);
742 msg (D_ROUTE, "%s", BSTR (&buf));
743 status = system_check (BSTR (&buf), es, 0, "ERROR: Solaris route add command failed");
745 #elif defined(TARGET_FREEBSD)
747 buf_printf (&buf, ROUTE_PATH " add");
749 #if 0
750 if (r->metric_defined)
751 buf_printf (&buf, " -rtt %d", r->metric);
752 #endif
754 buf_printf (&buf, " -net %s %s %s",
755 network,
756 gateway,
757 netmask);
759 msg (D_ROUTE, "%s", BSTR (&buf));
760 status = system_check (BSTR (&buf), es, 0, "ERROR: FreeBSD route add command failed");
762 #elif defined(TARGET_DARWIN)
764 buf_printf (&buf, ROUTE_PATH " add");
766 #if 0
767 if (r->metric_defined)
768 buf_printf (&buf, " -rtt %d", r->metric);
769 #endif
771 buf_printf (&buf, " -net %s %s %s",
772 network,
773 gateway,
774 netmask);
776 msg (D_ROUTE, "%s", BSTR (&buf));
777 status = system_check (BSTR (&buf), es, 0, "ERROR: OS X route add command failed");
779 #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
781 buf_printf (&buf, ROUTE_PATH " add");
783 #if 0
784 if (r->metric_defined)
785 buf_printf (&buf, " -rtt %d", r->metric);
786 #endif
788 buf_printf (&buf, " -net %s %s -netmask %s",
789 network,
790 gateway,
791 netmask);
793 msg (D_ROUTE, "%s", BSTR (&buf));
794 status = system_check (BSTR (&buf), es, 0, "ERROR: OpenBSD/NetBSD route add command failed");
796 #else
797 msg (M_FATAL, "Sorry, but I don't know how to do 'route' commands on this operating system. Try putting your routes in a --route-up script");
798 #endif
800 done:
801 r->defined = status;
802 gc_free (&gc);
805 static void
806 delete_route (const struct route *r, const struct tuntap *tt, unsigned int flags, const struct env_set *es)
808 struct gc_arena gc;
809 struct buffer buf;
810 const char *network;
811 const char *netmask;
812 const char *gateway;
814 if (!r->defined)
815 return;
817 gc_init (&gc);
819 buf = alloc_buf_gc (256, &gc);
820 network = print_in_addr_t (r->network, 0, &gc);
821 netmask = print_in_addr_t (r->netmask, 0, &gc);
822 gateway = print_in_addr_t (r->gateway, 0, &gc);
824 #if defined(TARGET_LINUX)
825 #ifdef CONFIG_FEATURE_IPROUTE
826 buf_printf (&buf, IPROUTE_PATH " route del %s/%d",
827 network,
828 count_netmask_bits(netmask));
829 #else
831 buf_printf (&buf, ROUTE_PATH " del -net %s netmask %s",
832 network,
833 netmask);
834 #endif /*CONFIG_FEATURE_IPROUTE*/
835 if (r->metric_defined)
836 buf_printf (&buf, " metric %d", r->metric);
837 msg (D_ROUTE, "%s", BSTR (&buf));
838 system_check (BSTR (&buf), es, 0, "ERROR: Linux route delete command failed");
840 #elif defined (WIN32)
842 buf_printf (&buf, ROUTE_PATH " DELETE %s MASK %s %s",
843 network,
844 netmask,
845 gateway);
847 msg (D_ROUTE, "%s", BSTR (&buf));
849 if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_IPAPI)
851 const bool status = del_route_ipapi (r, tt);
852 msg (D_ROUTE, "Route deletion via IPAPI %s", status ? "succeeded" : "failed");
854 else if ((flags & ROUTE_METHOD_MASK) == ROUTE_METHOD_EXE)
856 netcmd_semaphore_lock ();
857 system_check (BSTR (&buf), es, 0, "ERROR: Windows route delete command failed");
858 netcmd_semaphore_release ();
860 else
862 ASSERT (0);
865 #elif defined (TARGET_SOLARIS)
867 buf_printf (&buf, ROUTE_PATH " delete %s -netmask %s %s",
868 network,
869 netmask,
870 gateway);
872 msg (D_ROUTE, "%s", BSTR (&buf));
873 system_check (BSTR (&buf), es, 0, "ERROR: Solaris route delete command failed");
875 #elif defined(TARGET_FREEBSD)
877 buf_printf (&buf, ROUTE_PATH " delete -net %s %s %s",
878 network,
879 gateway,
880 netmask);
882 msg (D_ROUTE, "%s", BSTR (&buf));
883 system_check (BSTR (&buf), es, 0, "ERROR: FreeBSD route delete command failed");
885 #elif defined(TARGET_DARWIN)
887 buf_printf (&buf, ROUTE_PATH " delete -net %s %s %s",
888 network,
889 gateway,
890 netmask);
892 msg (D_ROUTE, "%s", BSTR (&buf));
893 system_check (BSTR (&buf), es, 0, "ERROR: OS X route delete command failed");
895 #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
897 buf_printf (&buf, ROUTE_PATH " delete -net %s %s -netmask %s",
898 network,
899 gateway,
900 netmask);
902 msg (D_ROUTE, "%s", BSTR (&buf));
903 system_check (BSTR (&buf), es, 0, "ERROR: OpenBSD/NetBSD route delete command failed");
905 #else
906 msg (M_FATAL, "Sorry, but I don't know how to do 'route' commands on this operating system. Try putting your routes in a --route-up script");
907 #endif
909 gc_free (&gc);
913 * The --redirect-gateway option requires OS-specific code below
914 * to get the current default gateway.
917 #if defined(WIN32)
919 static const MIB_IPFORWARDTABLE *
920 get_windows_routing_table (struct gc_arena *gc)
922 ULONG size = 0;
923 PMIB_IPFORWARDTABLE rt = NULL;
924 DWORD status;
926 status = GetIpForwardTable (NULL, &size, TRUE);
927 if (status == ERROR_INSUFFICIENT_BUFFER)
929 rt = (PMIB_IPFORWARDTABLE) gc_malloc (size, false, gc);
930 status = GetIpForwardTable (rt, &size, TRUE);
931 if (status != NO_ERROR)
933 msg (D_ROUTE, "NOTE: GetIpForwardTable returned error: %s (code=%u)",
934 strerror_win32 (status, gc),
935 (unsigned int)status);
936 rt = NULL;
939 return rt;
942 static int
943 test_route (const IP_ADAPTER_INFO *adapters,
944 const in_addr_t gateway,
945 DWORD *index)
947 int count = 0;
948 DWORD i = adapter_index_of_ip (adapters, gateway, &count);
949 if (index)
950 *index = i;
951 return count;
954 static void
955 test_route_helper (bool *ret,
956 int *count,
957 int *good,
958 int *ambig,
959 const IP_ADAPTER_INFO *adapters,
960 const in_addr_t gateway)
962 int c;
964 ++*count;
965 c = test_route (adapters, gateway, NULL);
966 if (c == 0)
967 *ret = false;
968 else
969 ++*good;
970 if (c > 1)
971 ++*ambig;
975 * If we tried to add routes now, would we succeed?
977 bool
978 test_routes (const struct route_list *rl, const struct tuntap *tt)
980 struct gc_arena gc = gc_new ();
981 const IP_ADAPTER_INFO *adapters = get_adapter_info_list (&gc);
982 bool ret = false;
983 int count = 0;
984 int good = 0;
985 int ambig = 0;
986 bool adapter_up = false;
988 if (is_adapter_up (tt, adapters))
990 ret = true;
991 adapter_up = true;
993 if (rl)
995 int i;
996 for (i = 0; i < rl->n; ++i)
997 test_route_helper (&ret, &count, &good, &ambig, adapters, rl->routes[i].gateway);
999 if (rl->redirect_default_gateway && rl->spec.remote_endpoint_defined)
1000 test_route_helper (&ret, &count, &good, &ambig, adapters, rl->spec.remote_endpoint);
1004 msg (D_ROUTE, "TEST ROUTES: %d/%d succeeded len=%d ret=%d a=%d u/d=%s",
1005 good,
1006 count,
1007 rl ? rl->n : -1,
1008 (int)ret,
1009 ambig,
1010 adapter_up ? "up" : "down");
1012 gc_free (&gc);
1013 return ret;
1016 static bool
1017 get_default_gateway (in_addr_t *gateway)
1019 struct gc_arena gc = gc_new ();
1020 int i;
1021 const MIB_IPFORWARDTABLE *routes = get_windows_routing_table (&gc);
1022 DWORD lowest_metric = ~0;
1023 in_addr_t ret = 0;
1024 int best = -1;
1026 if (!routes)
1027 goto done;
1029 for (i = 0; i < routes->dwNumEntries; ++i)
1031 const MIB_IPFORWARDROW *row = &routes->table[i];
1032 const in_addr_t net = ntohl (row->dwForwardDest);
1033 const in_addr_t mask = ntohl (row->dwForwardMask);
1034 const in_addr_t gw = ntohl (row->dwForwardNextHop);
1035 const DWORD metric = row->dwForwardMetric1;
1037 msg (D_ROUTE_DEBUG, "GDG: route[%d] %s %s %s m=%u",
1039 print_in_addr_t ((in_addr_t) net, 0, &gc),
1040 print_in_addr_t ((in_addr_t) mask, 0, &gc),
1041 print_in_addr_t ((in_addr_t) gw, 0, &gc),
1042 (unsigned int)metric);
1044 if (!net && !mask && metric < lowest_metric)
1046 ret = gw;
1047 lowest_metric = metric;
1048 best = i;
1052 done:
1053 gc_free (&gc);
1055 if (ret)
1057 dmsg (D_ROUTE_DEBUG, "GDGR: best=%d lm=%u", best, (unsigned int)lowest_metric);
1058 *gateway = ret;
1059 return true;
1061 else
1062 return false;
1065 static DWORD
1066 windows_route_find_if_index (const struct route *r, const struct tuntap *tt)
1068 struct gc_arena gc = gc_new ();
1069 DWORD ret = ~0;
1070 int count = 0;
1071 const IP_ADAPTER_INFO *adapters = get_adapter_info_list (&gc);
1072 const IP_ADAPTER_INFO *tun_adapter = get_tun_adapter (tt, adapters);
1073 bool on_tun = false;
1075 /* first test on tun interface */
1076 if (is_ip_in_adapter_subnet (tun_adapter, r->gateway, NULL))
1078 ret = tun_adapter->Index;
1079 count = 1;
1080 on_tun = true;
1082 else /* test on other interfaces */
1084 count = test_route (adapters, r->gateway, &ret);
1087 if (count == 0)
1089 msg (M_WARN, "Warning: route gateway is not reachable on any active network adapters: %s",
1090 print_in_addr_t (r->gateway, 0, &gc));
1091 ret = ~0;
1093 else if (count > 1)
1095 msg (M_WARN, "Warning: route gateway is ambiguous: %s (%d matches)",
1096 print_in_addr_t (r->gateway, 0, &gc),
1097 count);
1098 ret = ~0;
1101 dmsg (D_ROUTE_DEBUG, "DEBUG: route find if: on_tun=%d count=%d index=%d",
1102 on_tun,
1103 count,
1104 (int)ret);
1106 gc_free (&gc);
1107 return ret;
1110 bool
1111 add_route_ipapi (const struct route *r, const struct tuntap *tt)
1113 struct gc_arena gc = gc_new ();
1114 bool ret = false;
1115 DWORD status;
1116 const DWORD if_index = windows_route_find_if_index (r, tt);
1118 if (if_index != ~0)
1120 MIB_IPFORWARDROW fr;
1121 CLEAR (fr);
1122 fr.dwForwardDest = htonl (r->network);
1123 fr.dwForwardMask = htonl (r->netmask);
1124 fr.dwForwardPolicy = 0;
1125 fr.dwForwardNextHop = htonl (r->gateway);
1126 fr.dwForwardIfIndex = if_index;
1127 fr.dwForwardType = 4; /* the next hop is not the final dest */
1128 fr.dwForwardProto = 3; /* PROTO_IP_NETMGMT */
1129 fr.dwForwardAge = 0;
1130 fr.dwForwardNextHopAS = 0;
1131 fr.dwForwardMetric1 = r->metric_defined ? r->metric : 1;
1132 fr.dwForwardMetric2 = ~0;
1133 fr.dwForwardMetric3 = ~0;
1134 fr.dwForwardMetric4 = ~0;
1135 fr.dwForwardMetric5 = ~0;
1137 if ((r->network & r->netmask) != r->network)
1138 msg (M_WARN, "Warning: address %s is not a network address in relation to netmask %s",
1139 print_in_addr_t (r->network, 0, &gc),
1140 print_in_addr_t (r->netmask, 0, &gc));
1142 status = CreateIpForwardEntry (&fr);
1144 if (status == NO_ERROR)
1145 ret = true;
1146 else
1148 /* failed, try a different forward type (--redirect-gateway over RRAS seems to need this) */
1149 fr.dwForwardType = 3; /* the next hop is the final dest */
1151 status = CreateIpForwardEntry (&fr);
1153 if (status == NO_ERROR)
1154 ret = true;
1155 else
1156 msg (M_WARN, "ROUTE: route addition failed using CreateIpForwardEntry: %s [if_index=%u]",
1157 strerror_win32 (status, &gc),
1158 (unsigned int)if_index);
1162 gc_free (&gc);
1163 return ret;
1166 bool
1167 del_route_ipapi (const struct route *r, const struct tuntap *tt)
1169 struct gc_arena gc = gc_new ();
1170 bool ret = false;
1171 DWORD status;
1172 const DWORD if_index = windows_route_find_if_index (r, tt);
1174 if (if_index != ~0)
1176 MIB_IPFORWARDROW fr;
1177 CLEAR (fr);
1179 fr.dwForwardDest = htonl (r->network);
1180 fr.dwForwardMask = htonl (r->netmask);
1181 fr.dwForwardPolicy = 0;
1182 fr.dwForwardNextHop = htonl (r->gateway);
1183 fr.dwForwardIfIndex = if_index;
1185 status = DeleteIpForwardEntry (&fr);
1187 if (status == NO_ERROR)
1188 ret = true;
1189 else
1190 msg (M_WARN, "ROUTE: route deletion failed using DeleteIpForwardEntry: %s",
1191 strerror_win32 (status, &gc));
1194 gc_free (&gc);
1195 return ret;
1198 static const char *
1199 format_route_entry (const MIB_IPFORWARDROW *r, struct gc_arena *gc)
1201 struct buffer out = alloc_buf_gc (256, gc);
1202 buf_printf (&out, "%s %s %s p=%d i=%d t=%d pr=%d a=%d h=%d m=%d/%d/%d/%d/%d",
1203 print_in_addr_t (r->dwForwardDest, IA_NET_ORDER, gc),
1204 print_in_addr_t (r->dwForwardMask, IA_NET_ORDER, gc),
1205 print_in_addr_t (r->dwForwardNextHop, IA_NET_ORDER, gc),
1206 (int)r->dwForwardPolicy,
1207 (int)r->dwForwardIfIndex,
1208 (int)r->dwForwardType,
1209 (int)r->dwForwardProto,
1210 (int)r->dwForwardAge,
1211 (int)r->dwForwardNextHopAS,
1212 (int)r->dwForwardMetric1,
1213 (int)r->dwForwardMetric2,
1214 (int)r->dwForwardMetric3,
1215 (int)r->dwForwardMetric4,
1216 (int)r->dwForwardMetric5);
1217 return BSTR (&out);
1221 * Show current routing table
1223 void
1224 show_routes (int msglev)
1226 struct gc_arena gc = gc_new ();
1227 int i;
1229 const MIB_IPFORWARDTABLE *rt = get_windows_routing_table (&gc);
1231 msg (msglev, "SYSTEM ROUTING TABLE");
1232 if (rt)
1234 for (i = 0; i < rt->dwNumEntries; ++i)
1236 msg (msglev, "%s", format_route_entry (&rt->table[i], &gc));
1239 gc_free (&gc);
1242 #elif defined(TARGET_LINUX)
1244 static bool
1245 get_default_gateway (in_addr_t *gateway)
1247 struct gc_arena gc = gc_new ();
1248 bool ret = false;
1249 FILE *fp = fopen ("/proc/net/route", "r");
1250 if (fp)
1252 char line[256];
1253 int count = 0;
1254 int best_count = 0;
1255 unsigned int lowest_metric = ~0;
1256 in_addr_t best_gw = 0;
1257 while (fgets (line, sizeof (line), fp) != NULL)
1259 if (count)
1261 unsigned int net_x = 0;
1262 unsigned int mask_x = 0;
1263 unsigned int gw_x = 0;
1264 unsigned int metric = 0;
1265 const int np = sscanf (line, "%*s\t%x\t%x\t%*s\t%*s\t%*s\t%d\t%x",
1266 &net_x,
1267 &gw_x,
1268 &metric,
1269 &mask_x);
1270 if (np == 4)
1272 const in_addr_t net = ntohl (net_x);
1273 const in_addr_t mask = ntohl (mask_x);
1274 const in_addr_t gw = ntohl (gw_x);
1276 dmsg (D_ROUTE_DEBUG, "GDG: route[%d] %s/%s/%s m=%u",
1277 count,
1278 print_in_addr_t ((in_addr_t) net, 0, &gc),
1279 print_in_addr_t ((in_addr_t) mask, 0, &gc),
1280 print_in_addr_t ((in_addr_t) gw, 0, &gc),
1281 metric);
1283 if (!net && !mask && metric < lowest_metric)
1285 best_gw = gw;
1286 lowest_metric = metric;
1287 best_count = count;
1291 ++count;
1293 fclose (fp);
1295 if (best_gw)
1297 *gateway = best_gw;
1298 ret = true;
1301 dmsg (D_ROUTE_DEBUG, "GDG: best=%s[%d] lm=%u",
1302 print_in_addr_t ((in_addr_t) best_gw, 0, &gc),
1303 best_count,
1304 (unsigned int)lowest_metric);
1307 gc_free (&gc);
1308 return ret;
1311 #elif defined(TARGET_FREEBSD)
1313 #include <sys/types.h>
1314 #include <sys/socket.h>
1315 #include <netinet/in.h>
1317 /* all of this is taken from <net/route.h> in FreeBSD */
1318 #define RTA_DST 0x1
1319 #define RTA_GATEWAY 0x2
1320 #define RTA_NETMASK 0x4
1322 #define RTM_GET 0x4
1323 #define RTM_VERSION 5
1325 #define RTF_UP 0x1
1326 #define RTF_GATEWAY 0x2
1329 * These numbers are used by reliable protocols for determining
1330 * retransmission behavior and are included in the routing structure.
1332 struct rt_metrics {
1333 u_long rmx_locks; /* Kernel must leave these values alone */
1334 u_long rmx_mtu; /* MTU for this path */
1335 u_long rmx_hopcount; /* max hops expected */
1336 u_long rmx_expire; /* lifetime for route, e.g. redirect */
1337 u_long rmx_recvpipe; /* inbound delay-bandwidth product */
1338 u_long rmx_sendpipe; /* outbound delay-bandwidth product */
1339 u_long rmx_ssthresh; /* outbound gateway buffer limit */
1340 u_long rmx_rtt; /* estimated round trip time */
1341 u_long rmx_rttvar; /* estimated rtt variance */
1342 u_long rmx_pksent; /* packets sent using this route */
1343 u_long rmx_filler[4]; /* will be used for T/TCP later */
1347 * Structures for routing messages.
1349 struct rt_msghdr {
1350 u_short rtm_msglen; /* to skip over non-understood messages */
1351 u_char rtm_version; /* future binary compatibility */
1352 u_char rtm_type; /* message type */
1353 u_short rtm_index; /* index for associated ifp */
1354 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */
1355 int rtm_addrs; /* bitmask identifying sockaddrs in msg */
1356 pid_t rtm_pid; /* identify sender */
1357 int rtm_seq; /* for sender to identify action */
1358 int rtm_errno; /* why failed */
1359 int rtm_use; /* from rtentry */
1360 u_long rtm_inits; /* which metrics we are initializing */
1361 struct rt_metrics rtm_rmx; /* metrics themselves */
1364 struct {
1365 struct rt_msghdr m_rtm;
1366 char m_space[512];
1367 } m_rtmsg;
1369 #define ROUNDUP(a) \
1370 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1372 static bool
1373 get_default_gateway (in_addr_t *ret)
1375 struct gc_arena gc = gc_new ();
1376 int s, seq, l, pid, rtm_addrs, i;
1377 struct sockaddr so_dst, so_mask;
1378 char *cp = m_rtmsg.m_space;
1379 struct sockaddr *gate = NULL, *sa;
1380 struct rt_msghdr *rtm_aux;
1382 #define NEXTADDR(w, u) \
1383 if (rtm_addrs & (w)) {\
1384 l = ROUNDUP(u.sa_len); memmove(cp, &(u), l); cp += l;\
1387 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
1389 #define rtm m_rtmsg.m_rtm
1391 pid = getpid();
1392 seq = 0;
1393 rtm_addrs = RTA_DST | RTA_NETMASK;
1395 bzero(&so_dst, sizeof(so_dst));
1396 bzero(&so_mask, sizeof(so_mask));
1397 bzero(&rtm, sizeof(struct rt_msghdr));
1399 rtm.rtm_type = RTM_GET;
1400 rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
1401 rtm.rtm_version = RTM_VERSION;
1402 rtm.rtm_seq = ++seq;
1403 rtm.rtm_addrs = rtm_addrs;
1405 so_dst.sa_family = AF_INET;
1406 so_dst.sa_len = sizeof(struct sockaddr_in);
1407 so_mask.sa_family = AF_INET;
1408 so_mask.sa_len = sizeof(struct sockaddr_in);
1410 NEXTADDR(RTA_DST, so_dst);
1411 NEXTADDR(RTA_NETMASK, so_mask);
1413 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1415 s = socket(PF_ROUTE, SOCK_RAW, 0);
1417 if (write(s, (char *)&m_rtmsg, l) < 0)
1419 warn("writing to routing socket");
1420 gc_free (&gc);
1421 close(s);
1422 return false;
1425 do {
1426 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1427 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1429 close(s);
1431 rtm_aux = &rtm;
1433 cp = ((char *)(rtm_aux + 1));
1434 if (rtm_aux->rtm_addrs) {
1435 for (i = 1; i; i <<= 1)
1436 if (i & rtm_aux->rtm_addrs) {
1437 sa = (struct sockaddr *)cp;
1438 if (i == RTA_GATEWAY )
1439 gate = sa;
1440 ADVANCE(cp, sa);
1443 else
1445 gc_free (&gc);
1446 return false;
1450 if (gate != NULL )
1452 *ret = ntohl(((struct sockaddr_in *)gate)->sin_addr.s_addr);
1453 #if 1
1454 msg (M_INFO, "gw %s",
1455 print_in_addr_t ((in_addr_t) *ret, 0, &gc));
1456 #endif
1458 gc_free (&gc);
1459 return true;
1461 else
1463 gc_free (&gc);
1464 return false;
1468 #elif defined(TARGET_DARWIN)
1470 #include <sys/types.h>
1471 #include <sys/socket.h>
1472 #include <netinet/in.h>
1474 /* all of this is taken from <net/route.h> in Darwin */
1475 #define RTA_DST 0x1
1476 #define RTA_GATEWAY 0x2
1477 #define RTA_NETMASK 0x4
1479 #define RTM_GET 0x4
1480 #define RTM_VERSION 5
1482 #define RTF_UP 0x1
1483 #define RTF_GATEWAY 0x2
1486 * These numbers are used by reliable protocols for determining
1487 * retransmission behavior and are included in the routing structure.
1489 struct rt_metrics {
1490 u_long rmx_locks; /* Kernel must leave these values alone */
1491 u_long rmx_mtu; /* MTU for this path */
1492 u_long rmx_hopcount; /* max hops expected */
1493 u_long rmx_expire; /* lifetime for route, e.g. redirect */
1494 u_long rmx_recvpipe; /* inbound delay-bandwidth product */
1495 u_long rmx_sendpipe; /* outbound delay-bandwidth product */
1496 u_long rmx_ssthresh; /* outbound gateway buffer limit */
1497 u_long rmx_rtt; /* estimated round trip time */
1498 u_long rmx_rttvar; /* estimated rtt variance */
1499 u_long rmx_pksent; /* packets sent using this route */
1500 u_long rmx_filler[4]; /* will be used for T/TCP later */
1504 * Structures for routing messages.
1506 struct rt_msghdr {
1507 u_short rtm_msglen; /* to skip over non-understood messages */
1508 u_char rtm_version; /* future binary compatibility */
1509 u_char rtm_type; /* message type */
1510 u_short rtm_index; /* index for associated ifp */
1511 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */
1512 int rtm_addrs; /* bitmask identifying sockaddrs in msg */
1513 pid_t rtm_pid; /* identify sender */
1514 int rtm_seq; /* for sender to identify action */
1515 int rtm_errno; /* why failed */
1516 int rtm_use; /* from rtentry */
1517 u_long rtm_inits; /* which metrics we are initializing */
1518 struct rt_metrics rtm_rmx; /* metrics themselves */
1521 struct {
1522 struct rt_msghdr m_rtm;
1523 char m_space[512];
1524 } m_rtmsg;
1526 #define ROUNDUP(a) \
1527 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1529 static bool
1530 get_default_gateway (in_addr_t *ret)
1532 struct gc_arena gc = gc_new ();
1533 int s, seq, l, pid, rtm_addrs, i;
1534 struct sockaddr so_dst, so_mask;
1535 char *cp = m_rtmsg.m_space;
1536 struct sockaddr *gate = NULL, *sa;
1537 struct rt_msghdr *rtm_aux;
1539 #define NEXTADDR(w, u) \
1540 if (rtm_addrs & (w)) {\
1541 l = ROUNDUP(u.sa_len); memmove(cp, &(u), l); cp += l;\
1544 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
1546 #define rtm m_rtmsg.m_rtm
1548 pid = getpid();
1549 seq = 0;
1550 rtm_addrs = RTA_DST | RTA_NETMASK;
1552 bzero(&so_dst, sizeof(so_dst));
1553 bzero(&so_mask, sizeof(so_mask));
1554 bzero(&rtm, sizeof(struct rt_msghdr));
1556 rtm.rtm_type = RTM_GET;
1557 rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
1558 rtm.rtm_version = RTM_VERSION;
1559 rtm.rtm_seq = ++seq;
1560 rtm.rtm_addrs = rtm_addrs;
1562 so_dst.sa_family = AF_INET;
1563 so_dst.sa_len = sizeof(struct sockaddr_in);
1564 so_mask.sa_family = AF_INET;
1565 so_mask.sa_len = sizeof(struct sockaddr_in);
1567 NEXTADDR(RTA_DST, so_dst);
1568 NEXTADDR(RTA_NETMASK, so_mask);
1570 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1572 s = socket(PF_ROUTE, SOCK_RAW, 0);
1574 if (write(s, (char *)&m_rtmsg, l) < 0)
1576 msg (M_WARN, "ROUTE: problem writing to routing socket");
1577 gc_free (&gc);
1578 close(s);
1579 return false;
1582 do {
1583 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1584 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1586 close(s);
1588 rtm_aux = &rtm;
1590 cp = ((char *)(rtm_aux + 1));
1591 if (rtm_aux->rtm_addrs) {
1592 for (i = 1; i; i <<= 1)
1593 if (i & rtm_aux->rtm_addrs) {
1594 sa = (struct sockaddr *)cp;
1595 if (i == RTA_GATEWAY )
1596 gate = sa;
1597 ADVANCE(cp, sa);
1600 else
1602 gc_free (&gc);
1603 return false;
1607 if (gate != NULL )
1609 *ret = ntohl(((struct sockaddr_in *)gate)->sin_addr.s_addr);
1610 #if 1
1611 msg (M_INFO, "gw %s",
1612 print_in_addr_t ((in_addr_t) *ret, 0, &gc));
1613 #endif
1615 gc_free (&gc);
1616 return true;
1618 else
1620 gc_free (&gc);
1621 return false;
1625 #elif defined(TARGET_OPENBSD) || defined(TARGET_NETBSD)
1627 #include <sys/types.h>
1628 #include <sys/socket.h>
1629 #include <netinet/in.h>
1631 /* all of this is taken from <net/route.h> in OpenBSD 3.6 */
1632 #define RTA_DST 0x1 /* destination sockaddr present */
1633 #define RTA_GATEWAY 0x2 /* gateway sockaddr present */
1634 #define RTA_NETMASK 0x4 /* netmask sockaddr present */
1636 #define RTM_GET 0x4 /* Report Metrics */
1638 #define RTM_VERSION 3 /* Up the ante and ignore older versions */
1640 #define RTF_UP 0x1 /* route usable */
1641 #define RTF_GATEWAY 0x2 /* destination is a gateway */
1644 * Huge version for userland compatibility.
1646 struct rt_metrics {
1647 u_long rmx_locks; /* Kernel must leave these values alone */
1648 u_long rmx_mtu; /* MTU for this path */
1649 u_long rmx_hopcount; /* max hops expected */
1650 u_long rmx_expire; /* lifetime for route, e.g. redirect */
1651 u_long rmx_recvpipe; /* inbound delay-bandwidth product */
1652 u_long rmx_sendpipe; /* outbound delay-bandwidth product */
1653 u_long rmx_ssthresh; /* outbound gateway buffer limit */
1654 u_long rmx_rtt; /* estimated round trip time */
1655 u_long rmx_rttvar; /* estimated rtt variance */
1656 u_long rmx_pksent; /* packets sent using this route */
1660 * Structures for routing messages.
1662 struct rt_msghdr {
1663 u_short rtm_msglen; /* to skip over non-understood messages */
1664 u_char rtm_version; /* future binary compatibility */
1665 u_char rtm_type; /* message type */
1666 u_short rtm_index; /* index for associated ifp */
1667 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */
1668 int rtm_addrs; /* bitmask identifying sockaddrs in msg */
1669 pid_t rtm_pid; /* identify sender */
1670 int rtm_seq; /* for sender to identify action */
1671 int rtm_errno; /* why failed */
1672 int rtm_use; /* from rtentry */
1673 u_long rtm_inits; /* which metrics we are initializing */
1674 struct rt_metrics rtm_rmx; /* metrics themselves */
1677 struct {
1678 struct rt_msghdr m_rtm;
1679 char m_space[512];
1680 } m_rtmsg;
1682 #define ROUNDUP(a) \
1683 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
1685 static bool
1686 get_default_gateway (in_addr_t *ret)
1688 struct gc_arena gc = gc_new ();
1689 int s, seq, l, rtm_addrs, i;
1690 pid_t pid;
1691 struct sockaddr so_dst, so_mask;
1692 char *cp = m_rtmsg.m_space;
1693 struct sockaddr *gate = NULL, *sa;
1694 struct rt_msghdr *rtm_aux;
1696 #define NEXTADDR(w, u) \
1697 if (rtm_addrs & (w)) {\
1698 l = ROUNDUP(u.sa_len); memmove(cp, &(u), l); cp += l;\
1701 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
1703 #define rtm m_rtmsg.m_rtm
1705 pid = getpid();
1706 seq = 0;
1707 rtm_addrs = RTA_DST | RTA_NETMASK;
1709 bzero(&so_dst, sizeof(so_dst));
1710 bzero(&so_mask, sizeof(so_mask));
1711 bzero(&rtm, sizeof(struct rt_msghdr));
1713 rtm.rtm_type = RTM_GET;
1714 rtm.rtm_flags = RTF_UP | RTF_GATEWAY;
1715 rtm.rtm_version = RTM_VERSION;
1716 rtm.rtm_seq = ++seq;
1717 rtm.rtm_addrs = rtm_addrs;
1719 so_dst.sa_family = AF_INET;
1720 so_dst.sa_len = sizeof(struct sockaddr_in);
1721 so_mask.sa_family = AF_INET;
1722 so_mask.sa_len = sizeof(struct sockaddr_in);
1724 NEXTADDR(RTA_DST, so_dst);
1725 NEXTADDR(RTA_NETMASK, so_mask);
1727 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1729 s = socket(PF_ROUTE, SOCK_RAW, 0);
1731 if (write(s, (char *)&m_rtmsg, l) < 0)
1733 warn("writing to routing socket");
1734 gc_free (&gc);
1735 close(s);
1736 return false;
1739 do {
1740 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1741 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1743 close(s);
1745 rtm_aux = &rtm;
1747 cp = ((char *)(rtm_aux + 1));
1748 if (rtm_aux->rtm_addrs) {
1749 for (i = 1; i; i <<= 1)
1750 if (i & rtm_aux->rtm_addrs) {
1751 sa = (struct sockaddr *)cp;
1752 if (i == RTA_GATEWAY )
1753 gate = sa;
1754 ADVANCE(cp, sa);
1757 else
1759 gc_free (&gc);
1760 return false;
1764 if (gate != NULL )
1766 *ret = ntohl(((struct sockaddr_in *)gate)->sin_addr.s_addr);
1767 #if 1
1768 msg (M_INFO, "gw %s",
1769 print_in_addr_t ((in_addr_t) *ret, 0, &gc));
1770 #endif
1772 gc_free (&gc);
1773 return true;
1775 else
1777 gc_free (&gc);
1778 return false;
1782 #else
1784 static bool
1785 get_default_gateway (in_addr_t *ret)
1787 return false;
1790 #endif
1792 bool
1793 netmask_to_netbits (const in_addr_t network, const in_addr_t netmask, int *netbits)
1795 int i;
1796 const int addrlen = sizeof (in_addr_t) * 8;
1798 if ((network & netmask) == network)
1800 for (i = 0; i <= addrlen; ++i)
1802 in_addr_t mask = netbits_to_netmask (i);
1803 if (mask == netmask)
1805 if (i == addrlen)
1806 *netbits = -1;
1807 else
1808 *netbits = i;
1809 return true;
1813 return false;