File: test_tuntap.py

package info (click to toggle)
python-asyncssh 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,464 kB
  • sloc: python: 40,306; makefile: 11
file content (757 lines) | stat: -rw-r--r-- 22,270 bytes parent folder | download
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# Copyright (c) 2024 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
#     http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
#    GNU General Public License, Version 2.0, or any later versions of
#    that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
#     Ron Frederick - initial implementation, API, and documentation

"""Unit tests for AsyncSSH TUN/TAP support"""

import asyncio
import builtins
import errno
import socket
import struct
import sys

from unittest import skipIf, skipUnless
from unittest.mock import patch

import asyncssh
from asyncssh.tuntap import IFF_FMT, LINUX_IFF_TUN

from .server import Server, ServerTestCase
from .util import asynctest

if sys.platform != 'win32': # pragma: no branch
    import fcntl


_orig_funcs = {}


class _TunTapSocketMock:
    """TunTap socket mock"""

    def ioctl(self, request, arg):
        """Ignore ioctl requests to bring interface up"""

        # pylint: disable=no-self-use,unused-argument

        return arg

    def close(self):
        """Close this mock"""

        # pylint: disable=no-self-use


class _TunTapMock:
    """Common TUN/TAP mock"""

    _from_intf = {}

    def __init__(self, interface=None):
        if interface in self._from_intf:
            raise OSError(errno.EBUSY, 'Device busy')

        self._loop = asyncio.get_event_loop()

        self._sock1, self._sock2 = socket.socketpair(type=socket.SOCK_DGRAM)
        self._sock2.setblocking(False)

        self._interface = interface

        if interface:
            self._from_intf[interface] = self

    @classmethod
    def lookup_intf(cls, interface):
        """Look up mock by interface"""

        return cls._from_intf[interface]

    def fileno(self):
        """Return the fileno of sock1"""

        return self._sock1.fileno()

    def setblocking(self, blocking):
        """Set blocking mode on the socket"""

        self._sock1.setblocking(blocking)

    async def get_packets(self, count):
        """Get packets written to the TUN/TAP"""

        return [await self._loop.sock_recv(self._sock2, 65536)
                for _ in range(count)]

    def put_packets(self, packets):
        """Put packets for the TUN/TAP to read"""

        for packet in packets:
            self._sock2.send(packet)

    def read(self, size=-1):
        """Read a packet"""

        return self._sock1.recv(size)

    def write(self, packet):
        """Write a packet"""

        return self._sock1.send(packet)

    def close(self):
        """Close this mock"""

        self._from_intf.pop(self._interface, None)

        self._sock2.send(b'')
        self._sock1.close()
        self._sock2.close()


class _TunTapOSXMock(_TunTapMock):
    """TunTapOSX mock"""

    disable = False

    def __init__(self, name):
        if self.disable:
            raise OSError(errno.ENOENT, 'No such device')

        interface = name[5:]

        if int(interface[3:]) >= 16:
            raise OSError(errno.ENOENT, 'No such device')

        super().__init__(interface)


class _DarwinUTunMock(_TunTapMock):
    """Darwin UTun mock"""

    _AF_INET_PREFIX = socket.AF_INET.to_bytes(4, 'big')

    def __init__(self):
        super().__init__()

        self._unit = None

    def ioctl(self, request, arg):
        """Respond to DARWIN_CTLIOCGINFO request"""

        # pylint: disable=no-self-use,unused-argument

        return arg

    def connect(self, addr):
        """Connect to requested unit"""

        _, unit = addr

        if unit == 0:
            for unit in range(16):
                interface = f'utun{unit}'

                if interface not in self._from_intf:
                    break
            else:
                raise OSError(errno.EBUSY, 'No utun devices available')
        elif unit <= 16:
            unit -= 1
            interface = f'utun{unit}'

            if interface in self._from_intf:
                raise OSError(errno.EBUSY, 'Device busy')
        else:
            raise OSError(errno.ENOENT, 'No such device')

        self._unit = unit
        self._interface = interface
        self._from_intf[interface] = self

        return 0

    def getpeername(self):
        """Return utun unit"""

        return (0, self._unit + 1)

    def send(self, packet):
        """Send a packet"""

        return super().write(packet[4:])

    def recv(self, size):
        """Receive a packet"""

        return self._AF_INET_PREFIX + self.read(size)


class _LinuxMock(_TunTapMock):
    """Linux TUN/TAP mock"""

    def __init__(self):
        super().__init__()

        self._sock1.setblocking(False)

    def ioctl(self, request, arg):
        """Respond to LINUX_TUNSETIFF request"""

        # pylint: disable=unused-argument

        name, flags = struct.unpack(IFF_FMT, arg)

        if name[0] == 0:
            prefix = 'tun' if flags & LINUX_IFF_TUN else 'tap'

            for unit in range(16):
                interface = f'{prefix}{unit}'

                if interface not in self._from_intf:
                    break
            else:
                self.close()
                raise OSError(errno.EBUSY, 'No tun devices available')

            arg = struct.pack(IFF_FMT, interface.encode(), flags)
        else:
            interface = name.strip(b'\0').decode()
            unit = int(interface[3:])

            if unit >= 16:
                raise OSError(errno.ENOENT, 'No such device')

        self._interface = interface
        self._from_intf[interface] = self

        return arg

    def read(self, size=-1):
        """Read a packet"""

        try:
            return super().read(size)
        except BlockingIOError:
            return None


def _open(name, mode, *args, **kwargs):
    """Mock file open"""

    name = str(name)

    if name.startswith('/dev/tun') or name.startswith('/dev/tap'):
        return _TunTapOSXMock(name)
    elif name == '/dev/net/tun':
        return _LinuxMock()
    else:
        return _orig_funcs['open'](name, mode, *args, **kwargs)


# pylint: disable=redefined-builtin
def _socket(family=socket.AF_INET, type=socket.SOCK_STREAM,
            proto=0, fileno=None):
    """Mock socket creation"""

    if hasattr(socket, 'PF_SYSTEM') and family == socket.PF_SYSTEM and \
            type == socket.SOCK_DGRAM and proto == socket.SYSPROTO_CONTROL:
        return _DarwinUTunMock()
    elif family == socket.AF_INET and type == socket.SOCK_DGRAM:
        return _TunTapSocketMock()
    else:
        return _orig_funcs['socket'](family, type, proto, fileno)


def _ioctl(file, request, arg):
    """Mock ioctl"""

    if isinstance(file, (_DarwinUTunMock, _LinuxMock, _TunTapSocketMock)):
        return file.ioctl(request, arg)
    else: # pragma: no cover
        return _orig_funcs['ioctl'](file, request, arg)


async def get_packets(interface, count):
    """Return TUN/TAP packets written"""

    return await _TunTapMock.lookup_intf(interface).get_packets(count)


def put_packets(interface, packets):
    """Feed packets to a TUN/TAP mock"""

    _TunTapMock.lookup_intf(interface).put_packets(packets)


def patch_tuntap(cls):
    """Decorator to stub out TUN/TAP functions"""

    _orig_funcs['open'] = builtins.open
    _orig_funcs['socket'] = socket.socket

    cls = patch('builtins.open', _open)(cls)
    cls = patch('socket.socket', _socket)(cls)

    if sys.platform != 'win32': # pragma: no branch
        _orig_funcs['ioctl'] = fcntl.ioctl
        cls = patch('fcntl.ioctl', _ioctl)(cls)

    return cls


class _EchoSession(asyncssh.SSHTunTapSession):
    """Echo packets on a TUN session"""

    def __init__(self):
        self._chan = None

    def connection_made(self, chan):
        """Handle session open"""

        self._chan = chan

    def data_received(self, data, datatype):
        """Handle data from the channel"""

        self._chan.write(data)

    def eof_received(self):
        """Handle EOF from the channel"""

        self._chan.write_eof()


class _TunTapServer(Server):
    """Server for testing TUN/TAP functions"""

    async def _echo_handler(self, reader, writer):
        """Echo packets on a TUN session"""

        try:
            async for packet in reader:
                writer.write(packet)
        finally:
            writer.close()

    def tun_requested(self, unit):
        """Handle TUN requests"""

        if unit is None or unit <= 32:
            return True
        elif unit == 33:
            return _EchoSession()
        elif unit == 34:
            return (self._conn.create_tuntap_channel(), _EchoSession())
        elif unit == 35:
            return self._echo_handler
        else:
            return False

    def tap_requested(self, unit):
        """Handle TAP requests"""

        if unit == 33:
            return _EchoSession()
        else:
            return True


class _UpstreamForwardingServer(Server):
    """Server for testing forwarding between SSH connections"""

    def __init__(self, upstream_conn):
        super().__init__()

        self._upstream_conn = upstream_conn

    def tun_requested(self, unit):
        """Handle a request to create a new layer 3 tunnel"""

        return self._upstream_conn

    def tap_requested(self, unit):
        """Handle a request to create a new layer 2 tunnel"""

        return self._upstream_conn


@skipIf(sys.platform == 'win32', 'skip TUN/TAP tests on Windows')
@patch_tuntap
class _TestTunTap(ServerTestCase):
    """Unit tests for TUN/TAP functions"""

    @classmethod
    async def start_server(cls):
        """Start an SSH server to connect to"""

        return await cls.create_server(
            _TunTapServer, authorized_client_keys='authorized_keys')

    async def _check_tuntap(self, coro, interface):
        """Check sending data on a TUN or TAP channel"""

        reader, writer = await coro

        try:
            packets = [b'123', b'456', b'789']
            count = len(packets)

            for packet in packets:
                writer.write(packet)

            self.assertEqual((await get_packets(interface, count)), packets)

            put_packets(interface, packets)

            for packet in packets:
                self.assertEqual((await reader.read()), packet)
        finally:
            writer.close()

    async def _check_tuntap_forward(self, coro, remote_interface):
        """Check sending data on a TUN or TAP channel"""

        async with coro as forw:
            local_interface = forw.get_extra_info('interface')

            packets = [b'123', b'456', b'789']
            count = len(packets)

            put_packets(local_interface, packets)

            self.assertEqual((await get_packets(remote_interface, count)),
                              packets)

            put_packets(remote_interface, packets)

            self.assertEqual((await get_packets(local_interface, count)),
                              packets)

    async def _check_tuntap_echo(self, coro):
        """Check echoing of packets on a TUN channel"""

        reader, writer = await coro

        try:
            writer.write(b'123')
            self.assertEqual((await reader.read()), b'123')
            writer.write_eof()
            self.assertEqual((await reader.read()), b'')
        finally:
            writer.close()
            await writer.wait_closed()

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tun(self):
        """Test sending packets on a layer 3 tunnel on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tun(), 'tun0')

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tun_specific_unit(self):
        """Test sending on a layer 3 tunnel with specific unit on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tun(0), 'tun0')

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tun_error(self):
        """Test returning an open error on a layer 3 tunnel on macOS"""

        with self.assertRaises(asyncssh.ChannelOpenError):
            async with self.connect() as conn:
                await conn.open_tun(32)

    @skipUnless(sys.platform == 'darwin', 'only run utun tests on macOS')
    @asynctest
    async def test_darwin_open_utun(self):
        """Test sending packets on a layer 3 tunnel using UTun on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tun(16), 'utun0')

    @skipUnless(sys.platform == 'darwin', 'only run utun tests on macOS')
    @asynctest
    async def test_darwin_failover_to_utun(self):
        """Test failing over from TunTapOSX to UTun on macOS"""

        try:
            _TunTapOSXMock.disable = True

            async with self.connect() as conn:
                await self._check_tuntap(conn.open_tun(), 'utun0')
        finally:
            _TunTapOSXMock.disable = False

    @skipUnless(sys.platform == 'darwin', 'only run utun tests on macOS')
    @asynctest
    async def test_darwin_utun_in_use(self):
        """Test UTun device already in use on macOS"""

        async with self.connect() as conn:
            _, writer = await conn.open_tun(16)

            try:
                with self.assertRaises(asyncssh.ChannelOpenError):
                    await conn.open_tun(16)
            finally:
                writer.close()
                await writer.wait_closed()

    @skipUnless(sys.platform == 'darwin', 'only run utun tests on macOS')
    @asynctest
    async def test_darwin_utun_all_in_use(self):
        """Test all UTun devices already in use on macOS"""

        async with self.connect() as conn:
            writers = []

            try:
                for unit in range(32):
                    _, writer = await conn.open_tun(unit)
                    writers.append(writer)

                with self.assertRaises(asyncssh.ChannelOpenError):
                    await conn.open_tun()
            finally:
                for writer in writers:
                    writer.close()
                    await writer.wait_closed()

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tap(self):
        """Test sending packets on a layer 2 tunnel on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tap(), 'tap0')

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tap_unavailable(self):
        """Test TunTapOSX not being available on macOS"""

        try:
            _TunTapOSXMock.disable = True

            with self.assertRaises(asyncssh.ChannelOpenError):
                async with self.connect() as conn:
                    await conn.open_tap()
        finally:
            _TunTapOSXMock.disable = False

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_open_tap_error(self):
        """Test sending packets on a layer 2 tunnel on macOS"""

        with self.assertRaises(asyncssh.ChannelOpenError):
            async with self.connect() as conn:
                await conn.open_tap(16)

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_forward_tun(self):
        """Test forwarding packets on a layer 3 tunnel on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap_forward(conn.forward_tun(), 'tun0')

    @skipUnless(sys.platform == 'darwin', 'only run utun tests on macOS')
    @asynctest
    async def test_darwin_forward_utun(self):
        """Test forwarding packets on a layer 3 tunnel on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap_forward(conn.forward_tun(16, 17), 'utun1')

    @skipUnless(sys.platform == 'darwin', 'only run TapTunOSX tests on macOS')
    @asynctest
    async def test_darwin_forward_tap(self):
        """Test forwarding packets on a layer 2 tunnel on macOS"""

        async with self.connect() as conn:
            await self._check_tuntap_forward(conn.forward_tap(), 'tap0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_open_tun(self):
        """Test sending packets on a layer 3 tunnel on Linux"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tun(), 'tun0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_open_tun_specific_unit(self):
        """Test sending on a layer 3 tunnel with specific unit on Linux"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tun(), 'tun0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_open_tun_error(self):
        """Test returning an open error on a layer 3 tunnel on Linux"""

        with self.assertRaises(asyncssh.ChannelOpenError):
            async with self.connect() as conn:
                await conn.open_tun(32)

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_open_tap(self):
        """Test sending packets on a layer 2 tunnel on Linux"""

        async with self.connect() as conn:
            await self._check_tuntap(conn.open_tap(), 'tap0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_forward_tun(self):
        """Test forwarding packets on a layer 3 tunnel on Linux"""

        async with self.connect() as conn:
            await self._check_tuntap_forward(conn.forward_tun(), 'tun0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_forward_tap(self):
        """Test forwarding packets on a layer 2 tunnel on Linux"""

        async with self.connect() as conn:
            await self._check_tuntap_forward(conn.forward_tap(), 'tap0')

    @patch('sys.platform', 'linux')
    @asynctest
    async def test_linux_all_in_use(self):
        """Test all TUN devices already in use on Linux"""

        async with self.connect() as conn:
            writers = []

            try:
                for unit in range(16):
                    _, writer = await conn.open_tun(unit)
                    writers.append(writer)

                with self.assertRaises(asyncssh.ChannelOpenError):
                    await conn.open_tun()
            finally:
                for writer in writers:
                    writer.close()
                    await writer.wait_closed()

    @patch('sys.platform', 'xxx')
    @asynctest
    async def test_unknown_platform(self):
        """Test unknown platform"""

        async with self.connect() as conn:
            with self.assertRaises(asyncssh.ChannelOpenError):
                await conn.open_tun()

    @asynctest
    async def test_open_tun_echo_session(self):
        """Test an echo session on a layer 3 tunnel"""

        async with self.connect() as conn:
            await self._check_tuntap_echo(conn.open_tun(33))

    @asynctest
    async def test_upstream_open_tun_echo_session(self):
        """Test an echo session on a forwarded layer 3 tunnel"""

        def upstream_server():
            """Return a server capable of forwarding between SSH connections"""

            return _UpstreamForwardingServer(upstream_conn)

        async with self.connect() as upstream_conn:
            upstream_listener = await self.create_server(upstream_server)
            upstream_port = upstream_listener.get_port()

            async with self.connect('127.0.0.1', upstream_port) as conn:
                await self._check_tuntap_echo(conn.open_tun(33))

            upstream_listener.close()

    @asynctest
    async def test_upstream_open_tap_echo_session(self):
        """Test an echo session on a forwarded layer 2 tunnel"""

        def upstream_server():
            """Return a server capable of forwarding between SSH connections"""

            return _UpstreamForwardingServer(upstream_conn)

        async with self.connect() as upstream_conn:
            upstream_listener = await self.create_server(upstream_server)
            upstream_port = upstream_listener.get_port()

            async with self.connect('127.0.0.1', upstream_port) as conn:
                await self._check_tuntap_echo(conn.open_tap(33))

            upstream_listener.close()

    @asynctest
    async def test_open_tun_echo_session_channel(self):
        """Test an echo session & channel on a layer 3 tunnel"""

        async with self.connect() as conn:
            await self._check_tuntap_echo(conn.open_tun(34))

    @asynctest
    async def test_open_tun_echo_handler(self):
        """Test an echo stream handler on a layer 3 tunnel"""

        async with self.connect() as conn:
            await self._check_tuntap_echo(conn.open_tun(35))

    @asynctest
    async def test_open_tun_denied(self):
        """Test returning an open error on a layer 3 tunnel"""

        with self.assertRaises(asyncssh.ChannelOpenError):
            async with self.connect() as conn:
                await conn.open_tun(36)

    @asynctest
    async def test_tun_forward_error(self):
        """Test returning a forward error on a layer 3 tunnel"""

        with self.assertRaises(asyncssh.ChannelOpenError):
            async with self.connect() as conn:
                await conn.forward_tun(36)

    @asynctest
    async def test_invalid_tun_mode(self):
        """Test sending an invalid mode in a TUN/TAP request"""

        async with self.connect() as conn:
            chan = conn.create_tuntap_channel()

            with self.assertRaises(asyncssh.ChannelOpenError):
                await chan.open(asyncssh.SSHTunTapSession, 32, 0)