summaryrefslogtreecommitdiffstats
path: root/src/VBox/NetworkServices/NAT/rtmon_linux.c
blob: 1ef37e34d0f56c20fde9a28063c81fb8d0b0bf8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* $Id: rtmon_linux.c $ */
/** @file
 * NAT Network - IPv6 default route monitor for Linux netlink.
 */

/*
 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


#define LOG_GROUP LOG_GROUP_NAT_SERVICE

#include "proxy.h"

#include <sys/types.h>          /* must come before linux/netlink */
#include <sys/socket.h>

#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <errno.h>
#include <string.h>
#include <unistd.h>


static int rtmon_check_defaults(const void *buf, size_t len);


/**
 * Read IPv6 routing table - Linux rtnetlink version.
 *
 * XXX: TODO: To avoid re-reading the table we should subscribe to
 * updates by binding a monitoring NETLINK_ROUTE socket to
 * sockaddr_nl::nl_groups = RTMGRP_IPV6_ROUTE.
 *
 * But that will provide updates only.  Documentation is scarce, but
 * from what I've seen it seems that to get accurate routing info the
 * monitoring socket needs to be created first, then full routing
 * table requested (easier to do via spearate socket), then monitoring
 * socket polled for input.  The first update(s) of the monitoring
 * socket may happen before full table is returned, so we can't just
 * count the defaults, we need to keep track of their { oif, gw } to
 * correctly ignore updates that are reported via monitoring socket,
 * but that are already reflected in the full routing table returned
 * in response to our request.
 */
int
rtmon_get_defaults(void)
{
    int rtsock;
    ssize_t nsent, ssize;
    int ndefrts;

    char *buf = NULL;
    size_t bufsize;

    struct {
        struct nlmsghdr nh;
        struct rtmsg rtm;
        char attrbuf[512];
    } rtreq;

    memset(&rtreq, 0, sizeof(rtreq));
    rtreq.nh.nlmsg_type = RTM_GETROUTE;
    rtreq.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
    rtreq.rtm.rtm_family = AF_INET6;
    rtreq.rtm.rtm_table = RT_TABLE_MAIN;
    rtreq.rtm.rtm_protocol = RTPROT_UNSPEC;

    rtreq.nh.nlmsg_len = NLMSG_SPACE(sizeof(rtreq.rtm));

    bufsize = 1024;
    ssize = bufsize;
    for (;;) {
        char *newbuf;
        int recverr;

        newbuf = (char *)realloc(buf, ssize);
        if (newbuf == NULL) {
            DPRINTF0(("rtmon: failed to %sallocate buffer\n",
                      buf == NULL ? "" : "re"));
            free(buf);
            return -1;
        }

        buf = newbuf;
        bufsize = ssize;

        /* it's easier to reopen than to flush */
        rtsock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
        if (rtsock < 0) {
            DPRINTF0(("rtmon: failed to create netlink socket: %s", strerror(errno)));
            free(buf);
            return -1;
        }

        nsent = send(rtsock, &rtreq, rtreq.nh.nlmsg_len, 0);
        if (nsent < 0) {
            DPRINTF0(("rtmon: RTM_GETROUTE failed: %s", strerror(errno)));
            close (rtsock);
            free(buf);
            return -1;
        }

        ssize = recv(rtsock, buf, bufsize, MSG_TRUNC);
        recverr = errno;
        close (rtsock);

        if (ssize < 0) {
            DPRINTF(("rtmon: failed to read RTM_GETROUTE response: %s",
                     strerror(recverr)));
            free(buf);
            return -1;
        }

        if ((size_t)ssize <= bufsize) {
            DPRINTF2(("rtmon: RTM_GETROUTE: %lu bytes\n",
                      (unsigned long)ssize));
            break;
        }

        DPRINTF2(("rtmon: RTM_GETROUTE: truncated %lu to %lu bytes, retrying\n",
                  (unsigned long)ssize, (unsigned long)bufsize));
        /* try again with larger buffer */
    }

    ndefrts = rtmon_check_defaults(buf, (size_t)ssize);
    free(buf);

    if (ndefrts == 0) {
        DPRINTF(("rtmon: no IPv6 default routes found\n"));
    }
    else {
        DPRINTF(("rtmon: %d IPv6 default route%s found\n",
                 ndefrts,
                 ndefrts == 1 || ndefrts == -1 ? "" : "s"));
    }

    return ndefrts;
}


/**
 * Scan netlink message in the buffer for IPv6 default route changes.
 */
static int
rtmon_check_defaults(const void *buf, size_t len)
{
    struct nlmsghdr *nh;
    int dfltdiff = 0;

    for (nh = (struct nlmsghdr *)buf;
         NLMSG_OK(nh, len);
         nh = NLMSG_NEXT(nh, len))
    {
        struct rtmsg *rtm;
        struct rtattr *rta;
        int attrlen;
        int delta = 0;
        const void *gwbuf;
        size_t gwlen;
        int oif;

        DPRINTF2(("nlmsg seq %d type %d flags 0x%x\n",
                  nh->nlmsg_seq, nh->nlmsg_type, nh->nlmsg_flags));

        if (nh->nlmsg_type == NLMSG_DONE) {
            break;
        }

        if (nh->nlmsg_type == NLMSG_ERROR) {
            struct nlmsgerr *ne = (struct nlmsgerr *)NLMSG_DATA(nh);
            DPRINTF2(("> error %d\n", ne->error));
            LWIP_UNUSED_ARG(ne);
            break;
        }

        if (nh->nlmsg_type < RTM_BASE || RTM_MAX <= nh->nlmsg_type) {
            /* shouldn't happen */
            DPRINTF2(("> not an RTM message!\n"));
            continue;
        }


        rtm = (struct rtmsg *)NLMSG_DATA(nh);
        attrlen = RTM_PAYLOAD(nh);

        if (nh->nlmsg_type == RTM_NEWROUTE) {
            delta = +1;
        }
        else if (nh->nlmsg_type == RTM_DELROUTE) {
            delta = -1;
        }
        else {
            /* shouldn't happen */
            continue;
        }

        /*
         * Is this an IPv6 default route in the main table?  (Local
         * table always has ::/0 reject route, hence the last check).
         */
        if (rtm->rtm_family == AF_INET6 /* should always be true */
            && rtm->rtm_dst_len == 0
            && rtm->rtm_table == RT_TABLE_MAIN)
        {
            dfltdiff += delta;
        }
        else {
            /* some other route change */
            continue;
        }


        gwbuf = NULL;
        gwlen = 0;
        oif = -1;

        for (rta = RTM_RTA(rtm);
             RTA_OK(rta, attrlen);
             rta = RTA_NEXT(rta, attrlen))
        {
            if (rta->rta_type == RTA_GATEWAY) {
                gwbuf = RTA_DATA(rta);
                gwlen = RTA_PAYLOAD(rta);
            }
            else if (rta->rta_type == RTA_OIF) {
                /* assert RTA_PAYLOAD(rta) == 4 */
                memcpy(&oif, RTA_DATA(rta), sizeof(oif));
            }
        }

        /* XXX: TODO: note that { oif, gw } was added/removed */
        LWIP_UNUSED_ARG(gwbuf);
        LWIP_UNUSED_ARG(gwlen);
        LWIP_UNUSED_ARG(oif);
    }

    return dfltdiff;
}