VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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