summaryrefslogtreecommitdiff
path: root/src/VirtualTap.cpp
blob: dbb8a960f1ee7f19527aecf1b7798492fc70ded0 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
 * Copyright (c)2013-2021 ZeroTier, Inc.
 *
 * Use of this software is governed by the Business Source License included
 * in the LICENSE.TXT file in the project's root directory.
 *
 * Change Date: 2026-01-01
 *
 * On the date above, in accordance with the Business Source License, use
 * of this software will be governed by version 2.0 of the Apache License.
 */
/****/

/**
 * @file
 *
 * Virtual Ethernet tap device and combined network stack driver
 */

#include "InetAddress.hpp"
#include "MAC.hpp"
#include "MulticastGroup.hpp"
#include "Mutex.hpp"
#include "OSUtils.hpp"
#include "lwip/etharp.h"
#include "lwip/ethip6.h"
#include "lwip/netif.h"
#include "lwip/sys.h"
#include "lwip/tcpip.h"
#include "netif/ethernet.h"

#ifdef LWIP_STATS
#include "lwip/stats.h"
#endif

#include "Events.hpp"
#include "VirtualTap.hpp"

#if defined(__WINDOWS__)
#include "synchapi.h"

#include <time.h>
#endif

#define ZTS_TAP_THREAD_POLLING_INTERVAL 50
#define LWIP_DRIVER_LOOP_INTERVAL       100

namespace ZeroTier {

extern Events* zts_events;

/**
 * Virtual tap device. ZeroTier will create one per joined network. It will
 * then be destroyed upon leaving the network.
 */
VirtualTap::VirtualTap(
    const char* homePath,
    const MAC& mac,
    unsigned int mtu,
    unsigned int metric,
    uint64_t net_id,
    void (*handler)(
        void*,
        void*,
        uint64_t,
        const MAC&,
        const MAC&,
        unsigned int,
        unsigned int,
        const void*,
        unsigned int),
    void* arg)
    : _handler(handler)
    , _homePath(homePath)
    , _arg(arg)
    , _initialized(false)
    , _enabled(true)
    , _run(true)
    , _mac(mac)
    , _mtu(mtu)
    , _net_id(net_id)
    , _phy(this, false, true)
{
    OSUtils::ztsnprintf(vtap_full_name, VTAP_NAME_LEN, "libzt-vtap-%llx", _net_id);
#ifndef __WINDOWS__
    ::pipe(_shutdownSignalPipe);
#endif
    // Start virtual tap thread and stack I/O loops
    _thread = Thread::start(this);
}

VirtualTap::~VirtualTap()
{
    _run = false;
#ifndef __WINDOWS__
    ::write(_shutdownSignalPipe[1], "\0", 1);
#endif
    _phy.whack();
    zts_lwip_remove_netif(netif4);
    netif4 = NULL;
    zts_lwip_remove_netif(netif6);
    netif6 = NULL;
    Thread::join(_thread);
#ifndef __WINDOWS__
    ::close(_shutdownSignalPipe[0]);
    ::close(_shutdownSignalPipe[1]);
#endif
}

void VirtualTap::lastConfigUpdate(uint64_t lastConfigUpdateTime)
{
    _lastConfigUpdateTime = lastConfigUpdateTime;
}

void VirtualTap::setEnabled(bool en)
{
    _enabled = en;
}

bool VirtualTap::enabled() const
{
    return _enabled;
}

void VirtualTap::setUserEventSystem(Events* events)
{
    _events = events;
}

bool VirtualTap::hasIpv4Addr()
{
    Mutex::Lock _l(_ips_m);
    std::vector<InetAddress>::iterator it(_ips.begin());
    while (it != _ips.end()) {
        if ((*it).isV4()) {
            return true;
        }
        ++it;
    }
    return false;
}

bool VirtualTap::hasIpv6Addr()
{
    Mutex::Lock _l(_ips_m);
    std::vector<InetAddress>::iterator it(_ips.begin());
    while (it != _ips.end()) {
        if ((*it).isV6()) {
            return true;
        }
        ++it;
    }
    return false;
}

bool VirtualTap::addIp(const InetAddress& ip)
{
    // TODO: Rewrite to allow for more addresses
    char ipbuf[128] = { 0 };
    /* Limit address assignments to one per type.
    This limitation can be removed if some changes
    are made in the netif driver. */
    if (ip.isV4() && hasIpv4Addr()) {
        ip.toString(ipbuf);
        // DEBUG_INFO("failed to add IP (%s), only one per type per netif
        // allowed\n", ipbuf);
        return false;
    }
    if (ip.isV6() && hasIpv6Addr()) {
        ip.toString(ipbuf);
        // DEBUG_INFO("failed to add IP (%s), only one per type per netif
        // allowed\n", ipbuf);
        return false;
    }

    Mutex::Lock _l(_ips_m);
    if (_ips.size() >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
        return false;
    }
    if (std::find(_ips.begin(), _ips.end(), ip) == _ips.end()) {
        zts_lwip_init_interface((void*)this, ip);
        _ips.push_back(ip);
        std::sort(_ips.begin(), _ips.end());
    }
    return true;
}

bool VirtualTap::removeIp(const InetAddress& ip)
{
    Mutex::Lock _l(_ips_m);
    if (std::find(_ips.begin(), _ips.end(), ip) != _ips.end()) {
        std::vector<InetAddress>::iterator i(std::find(_ips.begin(), _ips.end(), ip));
        zts_lwip_remove_address_from_netif((void*)this, ip);
        _ips.erase(i);
    }
    return true;
}

std::vector<InetAddress> VirtualTap::ips() const
{
    Mutex::Lock _l(_ips_m);
    return _ips;
}

void VirtualTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len)
{
    if (len && _enabled) {
        printf("to lwip len=%d\n", len);
        zts_lwip_eth_rx(this, from, to, etherType, data, len);
    }
}

void VirtualTap::scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed)
{
    std::vector<MulticastGroup> newGroups;
    Mutex::Lock _l(_multicastGroups_m);
    // TODO: get multicast subscriptions
    std::vector<InetAddress> allIps(ips());
    for (std::vector<InetAddress>::iterator ip(allIps.begin()); ip != allIps.end(); ++ip)
        newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));

    std::sort(newGroups.begin(), newGroups.end());

    for (std::vector<MulticastGroup>::iterator m(newGroups.begin()); m != newGroups.end(); ++m) {
        if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m))
            added.push_back(*m);
    }
    for (std::vector<MulticastGroup>::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) {
        if (! std::binary_search(newGroups.begin(), newGroups.end(), *m))
            removed.push_back(*m);
    }
    _multicastGroups.swap(newGroups);
}

void VirtualTap::setMtu(unsigned int mtu)
{
    _mtu = mtu;
}

void VirtualTap::threadMain() throw()
{
    fd_set readfds, nullfds;
    struct timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    FD_ZERO(&readfds);
    FD_ZERO(&nullfds);
    int nfds = (int)std::max(_shutdownSignalPipe[0], 0) + 1;
#if defined(__linux__)
    // pthread_setname_np(pthread_self(), vtap_full_name);
#endif
#if defined(__APPLE__)
    // pthread_setname_np(vtap_full_name);
#endif
    while (true) {
        FD_SET(_shutdownSignalPipe[0], &readfds);
        select(nfds, &readfds, &nullfds, &nullfds, &tv);
        // writes to shutdown pipe terminate thread
        if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) {
            break;
        }
#if defined(__WINDOWS__)
        Sleep(ZTS_TAP_THREAD_POLLING_INTERVAL);
#else
        struct timespec sleepValue = { 0, 0 };
        sleepValue.tv_nsec = ZTS_TAP_THREAD_POLLING_INTERVAL * 500000;
        nanosleep(&sleepValue, NULL);
#endif
    }
}

//----------------------------------------------------------------------------//
// Netif driver code for lwIP network stack                                   //
//----------------------------------------------------------------------------//

bool _has_exited = false;
bool _has_started = false;

// Used to generate enumerated lwIP interface names
int netifCount = 0;

// Lock to guard access to network stack state changes
Mutex lwip_state_m;

// Callback for when the TCPIP thread has been successfully started
static void zts_tcpip_init_done(void* arg)
{
    sys_sem_t* sem;
    sem = (sys_sem_t*)arg;
    zts_events->setState(ZTS_STATE_STACK_RUNNING);
    _has_started = true;
    // zts_events->enqueue(ZTS_EVENT_STACK_UP, NULL);
    sys_sem_signal(sem);
}

static void zts_main_lwip_driver_loop(void* arg)
{
#if defined(__linux__)
    // pthread_setname_np(pthread_self(), ZTS_LWIP_THREAD_NAME);
#endif
#if defined(__APPLE__)
    // pthread_setname_np(ZTS_LWIP_THREAD_NAME);
#endif
    sys_sem_t sem;
    LWIP_UNUSED_ARG(arg);
    if (sys_sem_new(&sem, 0) != ERR_OK) {
        // DEBUG_ERROR("failed to create semaphore");
    }
    tcpip_init(zts_tcpip_init_done, &sem);
    sys_sem_wait(&sem);
    // Main loop
    while (zts_events->getState(ZTS_STATE_STACK_RUNNING)) {
        zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL);
    }
    _has_exited = true;
    
    //
    // no need to check if event was enqueued since NULL is being passed
    //
    zts_events->enqueue(ZTS_EVENT_STACK_DOWN, NULL);
}

bool zts_lwip_is_up()
{
    Mutex::Lock _l(lwip_state_m);
    return zts_events->getState(ZTS_STATE_STACK_RUNNING);
}

void zts_lwip_driver_init()
{
    if (zts_lwip_is_up()) {
        return;
    }
    if (_has_exited) {
        return;
    }
    Mutex::Lock _l(lwip_state_m);
#if defined(__WINDOWS__)
    sys_init();   // Required for win32 init of critical sections
#endif
    sys_thread_new(
        ZTS_LWIP_THREAD_NAME,
        zts_main_lwip_driver_loop,
        NULL,
        DEFAULT_THREAD_STACKSIZE,
        DEFAULT_THREAD_PRIO);
}

void zts_lwip_driver_shutdown()
{
    if (_has_exited) {
        return;
    }
    Mutex::Lock _l(lwip_state_m);
    // Set flag to stop sending frames into the core
    zts_events->clrState(ZTS_STATE_STACK_RUNNING);
    // Wait until the main lwIP thread has exited
    if (_has_started) {
        while (! _has_exited) {
            zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL);
        }
    }
}

void zts_lwip_remove_netif(void* netif)
{
    if (! netif) {
        return;
    }
    struct netif* n = (struct netif*)netif;
    LOCK_TCPIP_CORE();
    netif_remove(n);
    netif_set_down(n);
    netif_set_link_down(n);
    UNLOCK_TCPIP_CORE();
}

signed char zts_lwip_eth_tx(struct netif* n, struct pbuf* p)
{
    if (! n) {
        return ERR_IF;
    }
    struct pbuf* q;
    char buf[ZT_MAX_MTU + 32] = { 0 };
    char* bufptr;
    int totalLength = 0;

    VirtualTap* tap = (VirtualTap*)n->state;
    bufptr = buf;
    for (q = p; q != NULL; q = q->next) {
        memcpy(bufptr, q->payload, q->len);
        bufptr += q->len;
        totalLength += q->len;
    }
    struct eth_hdr* ethhdr;
    ethhdr = (struct eth_hdr*)buf;

    MAC src_mac;
    MAC dest_mac;
    src_mac.setTo(ethhdr->src.addr, 6);
    dest_mac.setTo(ethhdr->dest.addr, 6);

    char* data = buf + sizeof(struct eth_hdr);
    int len = totalLength - sizeof(struct eth_hdr);
    int proto = Utils::ntoh((uint16_t)ethhdr->type);
    printf("from lwip len=%d\n", len);
    tap->_handler(tap->_arg, NULL, tap->_net_id, src_mac, dest_mac, proto, 0, data, len);

    return ERR_OK;
}

void zts_lwip_eth_rx(
    VirtualTap* tap,
    const MAC& from,
    const MAC& to,
    unsigned int etherType,
    const void* data,
    unsigned int len)
{
#ifdef LWIP_STATS
    stats_display();
#endif
    if (! zts_events->getState(ZTS_STATE_STACK_RUNNING)) {
        return;
    }
    struct pbuf *p, *q;
    struct eth_hdr ethhdr;
    from.copyTo(ethhdr.src.addr, 6);
    to.copyTo(ethhdr.dest.addr, 6);
    ethhdr.type = Utils::hton((uint16_t)etherType);

    p = pbuf_alloc(PBUF_RAW, (uint16_t)len + sizeof(struct eth_hdr), PBUF_RAM);
    if (! p) {
        // DEBUG_ERROR("dropped packet: unable to allocate memory for
        // pbuf");
        return;
    }
    // First pbuf gets Ethernet header at start
    q = p;
    if (q->len < sizeof(ethhdr)) {
        pbuf_free(p);
        p = NULL;
        // DEBUG_ERROR("dropped packet: first pbuf smaller than Ethernet
        // header");
        return;
    }
    // Copy frame data into pbuf
    const char* dataptr = reinterpret_cast<const char*>(data);
    memcpy(q->payload, &ethhdr, sizeof(ethhdr));
    int remainingPayloadSpace = q->len - sizeof(ethhdr);
    memcpy((char*)q->payload + sizeof(ethhdr), dataptr, remainingPayloadSpace);
    dataptr += remainingPayloadSpace;
    // Remaining pbufs (if any) get rest of data
    while ((q = q->next)) {
        memcpy(q->payload, dataptr, q->len);
        dataptr += q->len;
    }
    // Feed packet into stack
    int err;

    if (Utils::ntoh(ethhdr.type) == 0x800 || Utils::ntoh(ethhdr.type) == 0x806) {
        if (tap->netif4) {
            if ((err = ((struct netif*)tap->netif4)->input(p, (struct netif*)tap->netif4)) != ERR_OK) {
                // DEBUG_ERROR("packet input error (%d)", err);
                pbuf_free(p);
            }
        }
    }
    if (Utils::ntoh(ethhdr.type) == 0x86DD) {
        if (tap->netif6) {
            if ((err = ((struct netif*)tap->netif6)->input(p, (struct netif*)tap->netif6)) != ERR_OK) {
                // DEBUG_ERROR("packet input error (%d)", err);
                pbuf_free(p);
            }
        }
    }
}

bool zts_lwip_is_netif_up(void* n)
{
    if (! n) {
        return false;
    }
    LOCK_TCPIP_CORE();
    bool result = netif_is_up((struct netif*)n);
    UNLOCK_TCPIP_CORE();
    return result;
}

static err_t zts_netif_init4(struct netif* n)
{
    if (! n || ! n->state) {
        return ERR_IF;
    }
    // Called from core, no need to lock
    VirtualTap* tap = (VirtualTap*)(n->state);
    n->hwaddr_len = 6;
    n->name[0] = '4';
    n->name[1] = 'a' + netifCount;
    n->linkoutput = zts_lwip_eth_tx;
    n->output = etharp_output;
    n->mtu = std::min(LWIP_MTU, (int)tap->_mtu);
    n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6
               | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
    n->hwaddr_len = sizeof(n->hwaddr);
    tap->_mac.copyTo(n->hwaddr, n->hwaddr_len);
    return ERR_OK;
}

static err_t zts_netif_init6(struct netif* n)
{
    if (! n || ! n->state) {
        return ERR_IF;
    }
    n->hwaddr_len = sizeof(n->hwaddr);
    VirtualTap* tap = (VirtualTap*)(n->state);
    tap->_mac.copyTo(n->hwaddr, n->hwaddr_len);
    // Called from core, no need to lock
    n->hwaddr_len = 6;
    n->name[0] = '6';
    n->name[1] = 'a' + netifCount;
    n->linkoutput = zts_lwip_eth_tx;
    n->output_ip6 = ethip6_output;
    n->mtu = std::min(LWIP_MTU, (int)tap->_mtu);
    n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6
               | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
    return ERR_OK;
}

void zts_lwip_init_interface(void* tapref, const InetAddress& ip)
{
    char macbuf[ZTS_MAC_ADDRSTRLEN] = { 0 };

    VirtualTap* vtap = (VirtualTap*)tapref;
    struct netif* n = NULL;
    bool isNewNetif = false;

    if (ip.isV4()) {
        if (vtap->netif4) {
            n = (struct netif*)vtap->netif4;
        }
        else {
            n = new struct netif;
            isNewNetif = true;
            netifCount++;
        }

        static ip4_addr_t ip4, netmask, gw;
        IP4_ADDR(&gw, 127, 0, 0, 1);
        ip4.addr = *((u32_t*)ip.rawIpData());
        netmask.addr = *((u32_t*)ip.netmask().rawIpData());
        LOCK_TCPIP_CORE();
        // 打印输出 ip4 netmask gw 真正的 十进制值
        printf("ip4.addr=%d, netmask.addr=%d, gw.addr=%d\n", ip4.addr, netmask.addr, gw.addr);
        netif_add(n, &ip4, &netmask, &gw, (void*)vtap, zts_netif_init4, tcpip_input);
        vtap->netif4 = (void*)n;
        UNLOCK_TCPIP_CORE();
        snprintf(
            macbuf,
            ZTS_MAC_ADDRSTRLEN,
            "%02x:%02x:%02x:%02x:%02x:%02x",
            n->hwaddr[0],
            n->hwaddr[1],
            n->hwaddr[2],
            n->hwaddr[3],
            n->hwaddr[4],
            n->hwaddr[5]);
    }
    if (ip.isV6()) {
        if (vtap->netif6) {
            n = (struct netif*)vtap->netif6;
        }
        else {
            n = new struct netif;
            isNewNetif = true;
            netifCount++;
        }
        static ip6_addr_t ip6;
        memcpy(&(ip6.addr), ip.rawIpData(), sizeof(ip6.addr));
        LOCK_TCPIP_CORE();
        if (isNewNetif) {
            vtap->netif6 = (void*)n;
            
            //
            // was:
            // netif_add(n, NULL, NULL, NULL, (void*)vtap, zts_netif_init6, ethernet_input);
            //
            // but there is this documentation:
            // > With NO_SYS=0, this must be set to tcpip_input for all netif types (whether ethernet, PPP, slipif, etc.)
            // https://lwip.fandom.com/wiki/Writing_a_device_driver
            //
            // and also with ethernet_input and lwIP asserts turned on, we get:
            // Assertion "Function called without core lock" failed at line 236 in /Users/brenton/development/github/libzt/ext/lwip-contrib/ports/unix/port/sys_arch.c
            //
            netif_add(n, NULL, NULL, NULL, (void*)vtap, zts_netif_init6, tcpip_input);

            n->ip6_autoconfig_enabled = 1;
            vtap->_mac.copyTo(n->hwaddr, n->hwaddr_len);
            netif_create_ip6_linklocal_address(n, 1);
            netif_set_link_up(n);
            netif_set_up(n);
            netif_set_default(n);
        }
        netif_add_ip6_address(n, &ip6, NULL);
        n->output_ip6 = ethip6_output;
        UNLOCK_TCPIP_CORE();
        snprintf(
            macbuf,
            ZTS_MAC_ADDRSTRLEN,
            "%02x:%02x:%02x:%02x:%02x:%02x",
            n->hwaddr[0],
            n->hwaddr[1],
            n->hwaddr[2],
            n->hwaddr[3],
            n->hwaddr[4],
            n->hwaddr[5]);
    }
}

void zts_lwip_remove_address_from_netif(void* tapref, const InetAddress& ip)
{
    if (! tapref) {
        return;
    }
    VirtualTap* vtap = (VirtualTap*)tapref;
    struct netif* n = NULL;
    /* When true multi-homing is implemented this will need to
    be a bit more sophisticated */
    if (ip.isV4()) {
        if (vtap->netif4) {
            n = (struct netif*)vtap->netif4;
        }
    }
    if (ip.isV6()) {
        if (vtap->netif6) {
            n = (struct netif*)vtap->netif6;
        }
    }
    if (! n) {
        return;
    }
    zts_lwip_remove_netif(n);
}

}   // namespace ZeroTier