VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NAT/rtmon_bsd.c@ 77758

Last change on this file since 77758 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 3.1 KB
Line 
1/* $Id: rtmon_bsd.c 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * NAT Network - IPv6 default route monitor for BSD routing sockets.
4 */
5
6/*
7 * Copyright (C) 2013-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#define LOG_GROUP LOG_GROUP_NAT_SERVICE
20
21#include "proxy.h"
22
23#include <sys/types.h>
24#include <sys/socket.h>
25
26#include <net/if_dl.h>
27#include <net/route.h>
28
29#include <netinet/in.h>
30#include <netinet/ip6.h>
31
32#include <errno.h>
33#include <string.h>
34#include <unistd.h>
35
36
37/**
38 * Query IPv6 routing table - BSD routing sockets version.
39 *
40 * We don't actually monitor the routing socket for updates, and
41 * instead query the kernel each time.
42 *
43 * We take a shortcut and don't read the reply to our RTM_GET - if
44 * there's no default IPv6 route, write(2) will fail with ESRCH
45 * synchronously. In theory it may fail asynchronously and we should
46 * wait for the RTM_GET reply and check rt_msghdr::rtm_errno.
47 *
48 * KAME code in *BSD maintains internally a list of default routers
49 * that it learned from RAs, and installs only one of them into the
50 * routing table (actually, I'm not sure if BSD routing table can
51 * handle multiple routes to the same destination). One side-effect
52 * of this is that when manually configured route (e.g. teredo) is
53 * deleted, the system will lose its default route even when KAME IPv6
54 * has default router(s) in its internal list. Next RA will force the
55 * update, though.
56 *
57 * Solaris does expose multiple routes in the routing table and
58 * replies to RTM_GET with "default default".
59 */
60int
61rtmon_get_defaults(void)
62{
63 int rtsock;
64 struct req {
65 struct rt_msghdr rtm;
66 struct sockaddr_in6 dst;
67 struct sockaddr_in6 mask;
68 struct sockaddr_dl ifp;
69 } req;
70 ssize_t nsent;
71
72 rtsock = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
73 if (rtsock < 0) {
74 DPRINTF0(("rtmon: failed to create routing socket\n"));
75 return -1;
76 }
77
78 memset(&req, 0, sizeof(req));
79
80 req.rtm.rtm_type = RTM_GET;
81 req.rtm.rtm_version = RTM_VERSION;
82 req.rtm.rtm_msglen = sizeof(req);
83 req.rtm.rtm_seq = 0x12345;
84
85 req.rtm.rtm_flags = RTF_UP;
86 req.rtm.rtm_addrs = RTA_DST | RTA_NETMASK | RTA_IFP;
87
88 req.dst.sin6_family = AF_INET6;
89#if HAVE_SA_LEN
90 req.dst.sin6_len = sizeof(req.dst);
91#endif
92
93 req.mask.sin6_family = AF_INET6;
94#if HAVE_SA_LEN
95 req.mask.sin6_len = sizeof(req.mask);
96#endif
97
98 req.ifp.sdl_family = AF_LINK;
99#if HAVE_SA_LEN
100 req.ifp.sdl_len = sizeof(req.ifp);
101#endif
102
103 nsent = write(rtsock, &req, req.rtm.rtm_msglen);
104 if (nsent < 0) {
105 if (errno == ESRCH) {
106 /* there's no default route */
107 return 0;
108 }
109 else {
110 DPRINTF0(("rtmon: failed to send RTM_GET\n"));
111 return -1;
112 }
113 }
114
115 return 1;
116}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette