summaryrefslogtreecommitdiffstats
path: root/src/network/kernel/qnetworkinterface_linux.cpp
blob: 25aba5836e6c1b43f5752c6210d569365170f526 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/****************************************************************************
**
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qnetworkinterface.h"
#include "qnetworkinterface_p.h"
#include "qnetworkinterface_unix_p.h"

#ifndef QT_NO_NETWORKINTERFACE

#include <qendian.h>
#include <qobjectdefs.h>
#include <qvarlengtharray.h>

// accordding to rtnetlink(7)
#include <asm/types.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/wireless.h>
#include <sys/socket.h>

/* in case these aren't defined in linux/if_arp.h (added since 2.6.28)  */
#define ARPHRD_PHONET       820     /* v2.6.29: PhoNet media type */
#define ARPHRD_PHONET_PIPE  821     /* v2.6.29: PhoNet pipe header */
#define ARPHRD_IEEE802154   804     /* v2.6.31 */
#define ARPHRD_6LOWPAN      825     /* v3.14: IPv6 over LoWPAN */

QT_BEGIN_NAMESPACE

enum {
    BufferSize = 8192
};

static QNetworkInterface::InterfaceType probeIfType(int socket, struct ifreq *req, short arptype)
{
    switch (ushort(arptype)) {
    case ARPHRD_LOOPBACK:
        return QNetworkInterface::Loopback;

    case ARPHRD_ETHER:
        // check if it's a WiFi interface
        if (qt_safe_ioctl(socket, SIOCGIWMODE, req) >= 0)
            return QNetworkInterface::Wifi;
        return QNetworkInterface::Ethernet;

    case ARPHRD_SLIP:
    case ARPHRD_CSLIP:
    case ARPHRD_SLIP6:
    case ARPHRD_CSLIP6:
        return QNetworkInterface::Slip;

    case ARPHRD_CAN:
        return QNetworkInterface::CanBus;

    case ARPHRD_PPP:
        return QNetworkInterface::Ppp;

    case ARPHRD_FDDI:
        return QNetworkInterface::Fddi;

    case ARPHRD_IEEE80211:
    case ARPHRD_IEEE80211_PRISM:
    case ARPHRD_IEEE80211_RADIOTAP:
        return QNetworkInterface::Ieee80211;

    case ARPHRD_IEEE802154:
        return QNetworkInterface::Ieee802154;

    case ARPHRD_PHONET:
    case ARPHRD_PHONET_PIPE:
        return QNetworkInterface::Phonet;

    case ARPHRD_6LOWPAN:
        return QNetworkInterface::SixLoWPAN;

    case ARPHRD_TUNNEL:
    case ARPHRD_TUNNEL6:
    case ARPHRD_NONE:
    case ARPHRD_VOID:
        return QNetworkInterface::Virtual;
    }
    return QNetworkInterface::Unknown;
}


namespace {
struct NetlinkSocket
{
    int sock;
    NetlinkSocket(int bufferSize)
    {
        sock = qt_safe_socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
        if (Q_UNLIKELY(sock == -1))
            qErrnoWarning("Could not create AF_NETLINK socket");

        // set buffer length
        socklen_t len = sizeof(bufferSize);
        setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufferSize, len);
    }

    ~NetlinkSocket()
    {
        if (sock != -1)
            qt_safe_close(sock);
    }

    operator int() const { return sock; }
};

template <typename Lambda> struct ProcessNetlinkRequest
{
    using FunctionTraits = QtPrivate::FunctionPointer<decltype(&Lambda::operator())>;
    using FirstArgument = typename FunctionTraits::Arguments::Car;

    static int expectedTypeForRequest(int rtype)
    {
        Q_STATIC_ASSERT(RTM_NEWADDR == RTM_GETADDR - 2);
        Q_STATIC_ASSERT(RTM_NEWLINK == RTM_GETLINK - 2);
        Q_ASSERT(rtype == RTM_GETADDR || rtype == RTM_GETLINK);
        return rtype - 2;
    }

    void operator()(int sock, nlmsghdr *hdr, char *buf, size_t bufsize, Lambda &&func)
    {
        // send the request
        if (send(sock, hdr, hdr->nlmsg_len, 0) != ssize_t(hdr->nlmsg_len))
            return;

        // receive and parse the request
        int expectedType = expectedTypeForRequest(hdr->nlmsg_type);
        const bool isDump = hdr->nlmsg_flags & NLM_F_DUMP;
        forever {
            qsizetype len = recv(sock, buf, bufsize, 0);
            hdr = reinterpret_cast<struct nlmsghdr *>(buf);
            if (!NLMSG_OK(hdr, quint32(len)))
                return;

            auto arg = reinterpret_cast<FirstArgument>(NLMSG_DATA(hdr));
            size_t payloadLen = NLMSG_PAYLOAD(hdr, 0);

            // is this a multipart message?
            Q_ASSERT(isDump == !!(hdr->nlmsg_flags & NLM_F_MULTI));
            if (!isDump) {
                // no, single message
                if (hdr->nlmsg_type == expectedType && payloadLen >= sizeof(FirstArgument))
                    return void(func(arg, payloadLen));
            } else {
                // multipart, parse until done
                do {
                    if (hdr->nlmsg_type == NLMSG_DONE)
                        return;
                    if (hdr->nlmsg_type != expectedType || payloadLen < sizeof(FirstArgument))
                        break;
                    func(arg, payloadLen);

                    // NLMSG_NEXT also updates the len variable
                    hdr = NLMSG_NEXT(hdr, len);
                    arg = reinterpret_cast<FirstArgument>(NLMSG_DATA(hdr));
                    payloadLen = NLMSG_PAYLOAD(hdr, 0);
                } while (NLMSG_OK(hdr, quint32(len)));

                if (len == 0)
                    continue;       // get new datagram
            }

#ifndef QT_NO_DEBUG
            if (NLMSG_OK(hdr, quint32(len)))
                qWarning("QNetworkInterface/AF_NETLINK: received unknown packet type (%d) or too short (%u)",
                         hdr->nlmsg_type, hdr->nlmsg_len);
            else
                qWarning("QNetworkInterface/AF_NETLINK: received invalid packet with size %d", int(len));
#endif
            return;
        }
    }
};

template <typename Lambda>
void processNetlinkRequest(int sock, struct nlmsghdr *hdr, char *buf, size_t bufsize, Lambda &&l)
{
    ProcessNetlinkRequest<Lambda>()(sock, hdr, buf, bufsize, std::forward<Lambda>(l));
}
}

uint QNetworkInterfaceManager::interfaceIndexFromName(const QString &name)
{
    uint index = 0;
    if (name.length() >= IFNAMSIZ)
        return index;

    int socket = qt_safe_socket(AF_INET, SOCK_DGRAM, 0);
    if (socket >= 0) {
        struct ifreq req;
        req.ifr_ifindex = 0;
        strcpy(req.ifr_name, name.toLatin1().constData());

        if (qt_safe_ioctl(socket, SIOCGIFINDEX, &req) >= 0)
            index = req.ifr_ifindex;
        qt_safe_close(socket);
    }
    return index;
}

QString QNetworkInterfaceManager::interfaceNameFromIndex(uint index)
{
    int socket = qt_safe_socket(AF_INET, SOCK_DGRAM, 0);
    if (socket >= 0) {
        struct ifreq req;
        req.ifr_ifindex = index;

        if (qt_safe_ioctl(socket, SIOCGIFNAME, &req) >= 0) {
            qt_safe_close(socket);
            return QString::fromLatin1(req.ifr_name);
        }
        qt_safe_close(socket);
    }
    return QString();
}

static QList<QNetworkInterfacePrivate *> getInterfaces(int sock, char *buf)
{
    QList<QNetworkInterfacePrivate *> result;
    struct ifreq req;

    // request all links
    struct {
        struct nlmsghdr req;
        struct ifinfomsg ifi;
    } ifi_req;
    memset(&ifi_req, 0, sizeof(ifi_req));

    ifi_req.req.nlmsg_len = sizeof(ifi_req);
    ifi_req.req.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
    ifi_req.req.nlmsg_type = RTM_GETLINK;

    // parse the interfaces
    processNetlinkRequest(sock, &ifi_req.req, buf, BufferSize, [&](ifinfomsg *ifi, size_t len) {
        auto iface = new QNetworkInterfacePrivate;
        iface->index = ifi->ifi_index;
        iface->flags = convertFlags(ifi->ifi_flags);

        // read attributes
        auto rta = reinterpret_cast<struct rtattr *>(ifi + 1);
        len -= sizeof(*ifi);
        for ( ; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
            int payloadLen = RTA_PAYLOAD(rta);
            auto payloadPtr = reinterpret_cast<char *>(RTA_DATA(rta));

            switch (rta->rta_type) {
            case IFLA_ADDRESS:      // link-level address
                iface->hardwareAddress =
                        iface->makeHwAddress(payloadLen, reinterpret_cast<uchar *>(payloadPtr));
                break;

            case IFLA_IFNAME:       // interface name
                Q_ASSERT(payloadLen <= int(sizeof(req.ifr_name)));
                memcpy(req.ifr_name, payloadPtr, payloadLen);   // including terminating NUL
                iface->name = QString::fromLatin1(payloadPtr, payloadLen - 1);
                break;

            case IFLA_MTU:
                Q_ASSERT(payloadLen == sizeof(int));
                iface->mtu = *reinterpret_cast<int *>(payloadPtr);
                break;

            case IFLA_OPERSTATE:    // operational state
                if (*payloadPtr != IF_OPER_UNKNOWN) {
                    // override the flag
                    iface->flags &= ~QNetworkInterface::IsUp;
                    if (*payloadPtr == IF_OPER_UP)
                        iface->flags |= QNetworkInterface::IsUp;
                }
                break;
            }
        }

        if (Q_UNLIKELY(iface->name.isEmpty())) {
            qWarning("QNetworkInterface: found interface %d with no name", iface->index);
            delete iface;
        } else {
            iface->type = probeIfType(sock, &req, ifi->ifi_type);
            result.append(iface);
        }
    });
    return result;
}

static void getAddresses(int sock, char *buf, QList<QNetworkInterfacePrivate *> &result)
{
    // request all addresses
    struct {
        struct nlmsghdr req;
        struct ifaddrmsg ifa;
    } ifa_req;
    memset(&ifa_req, 0, sizeof(ifa_req));

    ifa_req.req.nlmsg_len = sizeof(ifa_req);
    ifa_req.req.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
    ifa_req.req.nlmsg_type = RTM_GETADDR;
    ifa_req.req.nlmsg_seq = 1;

    // parse the addresses
    processNetlinkRequest(sock, &ifa_req.req, buf, BufferSize, [&](ifaddrmsg *ifa, size_t len) {
        if (Q_UNLIKELY(ifa->ifa_family != AF_INET && ifa->ifa_family != AF_INET6)) {
            // unknown address types
            return;
        }

        // find the interface this is relevant to
        QNetworkInterfacePrivate *iface = nullptr;
        for (auto candidate : qAsConst(result)) {
            if (candidate->index != int(ifa->ifa_index))
                continue;
            iface = candidate;
            break;
        }

        if (Q_UNLIKELY(!iface)) {
            qWarning("QNetworkInterface/AF_NETLINK: found unknown interface with index %d", ifa->ifa_index);
            return;
        }

        QNetworkAddressEntry entry;
        quint32 flags = ifa->ifa_flags;  // may be overwritten by IFA_FLAGS

        auto makeAddress = [=](uchar *ptr, int len) {
            QHostAddress addr;
            if (ifa->ifa_family == AF_INET) {
                Q_ASSERT(len == 4);
                addr.setAddress(qFromBigEndian<quint32>(ptr));
            } else {
                Q_ASSERT(len == 16);
                addr.setAddress(ptr);

                // do we need a scope ID?
                if (addr.isLinkLocal())
                    addr.setScopeId(iface->name);
            }
            return addr;
        };

        // read attributes
        auto rta = reinterpret_cast<struct rtattr *>(ifa + 1);
        len -= sizeof(*ifa);
        for ( ; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) {
            int payloadLen = RTA_PAYLOAD(rta);
            auto payloadPtr = reinterpret_cast<uchar *>(RTA_DATA(rta));

            switch (rta->rta_type) {
            case IFA_ADDRESS:
                // Local address (all interfaces except for point-to-point)
                if (entry.ip().isNull())
                    entry.setIp(makeAddress(payloadPtr, payloadLen));
                break;

            case IFA_LOCAL:
                // Override the local address (point-to-point interfaces)
                entry.setIp(makeAddress(payloadPtr, payloadLen));
                break;

            case IFA_BROADCAST:
                Q_ASSERT(ifa->ifa_family == AF_INET);
                entry.setBroadcast(makeAddress(payloadPtr, payloadLen));
                break;

            case IFA_CACHEINFO:
                if (size_t(payloadLen) >= sizeof(ifa_cacheinfo)) {
                    auto cacheinfo = reinterpret_cast<ifa_cacheinfo *>(payloadPtr);
                    auto toDeadline = [](quint32 lifetime) -> QDeadlineTimer {
                        if (lifetime == quint32(-1))
                            return QDeadlineTimer::Forever;
                        return QDeadlineTimer(lifetime * 1000);
                    };
                    entry.setAddressLifetime(toDeadline(cacheinfo->ifa_prefered), toDeadline(cacheinfo->ifa_valid));
                }
                break;

            case IFA_FLAGS:
                Q_ASSERT(payloadLen == 4);
                flags = qFromUnaligned<quint32>(payloadPtr);
                break;
            }
        }

        // now handle flags
        QNetworkInterfacePrivate::calculateDnsEligibility(&entry,
                                                          flags & IFA_F_TEMPORARY,
                                                          flags & IFA_F_DEPRECATED);


        if (!entry.ip().isNull()) {
            entry.setPrefixLength(ifa->ifa_prefixlen);
            iface->addressEntries.append(entry);
        }
    });
}

QList<QNetworkInterfacePrivate *> QNetworkInterfaceManager::scan()
{
    // open netlink socket
    QList<QNetworkInterfacePrivate *> result;
    NetlinkSocket sock(BufferSize);
    if (Q_UNLIKELY(sock == -1))
        return result;

    QByteArray buffer(BufferSize, Qt::Uninitialized);
    char *buf = buffer.data();

    result = getInterfaces(sock, buf);
    getAddresses(sock, buf, result);

    return result;
}

QT_END_NAMESPACE

#endif // QT_NO_NETWORKINTERFACE