This patch adds a file (rawnetarch_tuntap.c) implementing Ethernet support via the Universal TUN/TAP driver.
With this patch, regular users (without root privileges) can run VICE with Ethernet support. Furthermore, VICE will only see the Ethernet frames belonging to the (virtual) Ethernet interface it attaches to: this removes the security issues of the current Ethernet support in VICE, which uses root+libpcap and sees all network traffic.
The patch is developed and tested under Linux, but it should work on any Unix-like OS supporting TUN/TAP (e.g., NetBSD, FreeBSD) with minor tweaks. On the emulated side, I tested it by running various versions of Contiki (1.2-devel1, 3.0, and 2019-05-08) with RR-Net.
These instructions are tested on Ubuntu 20.10, but they should work on any Linux distribution.
Root privileges are only needed to set up a TUN/TAP virtual Ethernet device accessible by a regular user. For example:
# tunctl -u retro -t tap-c64
# ip link set tap-c64 up
# ip addr add 192.168.64.1/24 dev tap-c64
The commands above have the following effect:
tap-c64virtual network interface, giving access to user retro192.168.64.1/24 to the tap-c64 interfaceNow, the tap-c64 interface can act as gateway for a private network (192.168.64.X) where VICE-emulated computers can connect.
To give Internet access to the emulated computers, it is necessary to also enable IP forwarding and masquerading:
# echo 1 > /proc/sys/net/ipv4/ip_forward
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Let's say that user retro wants to run VICE with Ethernet (RR-Net) emulation. The vicerc file should contain:
ETHERNET_INTERFACE="tap-c64"
ETHERNETCART_ACTIVE=1
ETHERNETCARTMode=1
Now, retro can simply run e.g.:
$ x64sc
and set up networking in the emulated C64, under the tap-c64 private network. For example:
192.168.64.64255.255.255.0192.168.64.11.1.1.1At compile-time, the patch checks whether the macro HAVE_TUNTAP is defined --- and if so, it disables rawnetarch_unix.cand enables the new rawnetarch_tuntap.c.
The patch contains a few UI-related changes, to let non-root users have Ethernet support if TUN/TAP is available. It also tweaks some user-visible error messages, to mention TUN/TAP.
At the moment, the name of the TUN/TAP network interface must be specified in vicerc(as in the example above), or using the -cs8900ioif command line option. The GUI would need some additional tweaks: I can look into the GTK3 UI --- if this patch is deemed interesting :-)
we had tun/tap support in VICE before... it was removed because it broke rawnet support. it should be possible to have either, exclusively or both. we cant replace rawnet support with tuntap, because not all kernals have it. so, this patch should be reworked in a way that when HAVE_TUNTAP is enabled, it should enable this as an option, not replace rawnet alltogether (which also makes it kinda annoying to test both on the same machine).
Last edit: gpz 2020-12-29
Yes, the current version of the patch is quite rough --- this is what worked for me, and I wanted to see whether somebody else could find it useful :-)
So, I understand there is interest in re-integrating TUN/TAP support in VICE (as long as it doesn't come at the expense of libpcap support): I'll revise the patch.
yes, there is definetly interest in merging it again. IF it works without breaking anything that is already working :)
please also make sure you are respecting the codestyle outlined in doc/coding-guidelines.txt ... i have quickly skimmed over it and it seems to be mostly ok. one thing i noticed are missing spaces before and after operators in one or two lines, eg
should be
Then for bonus points, provide the documentation as a (seperate) diff to vice.texi :)
I will keep an eye on this for usability on NetBSD. There is much the same between Linux and NetBSD in how to use a tun or tap device, but there is much different in how to set it up for use (both in VICE as for the user). Don't worry too much about it though, it can be smoothed over :)
New year, new TUN/TAP patch!
How to configure VICE with TUN/TAP Ethernet support
Please see the instructions for the first patch --- with one key difference: this new patch has a new setting for
vicerc, i.e.,or, from the command line:
By writing
pcapinstead oftuntapabove, VICE will use the libpcap-based rawnet driver (if availble).If neither
ETHERNET_DRIVERnor-cs8900iodriveris specified, then:if the current user is
root, and libpcap is available, then VICE will use the libpcap-based rawnet driverotherwise (i.e., if the user is not
root, or libpcap is unavailable), VICE will use the new TUN/TAP-based rawnet driverThis means that:
users of VICE+libpcap+root will see no difference in how VICE behaves --- unless they explicitly add the setting
ETHERNET_DRIVER="tuntap"or-cs8900iodriver tuntapnon-root users will have the new TUN/TAP Ethernet support enabled by default
Another difference with the previous patch is that it is now possible to select the TUN/TAP interface from the VICE GUI.
How does it work?
Under Windows, this patch should have no effect:
rawnetarch_win32.cis unchanged, and still included as-is insrc/arch/shared/rawnetarch.c(NOTE: I could not test the patch under Windows).Under Unix, the patch turns
src/arch/shared/rawnetarch.cinto an abstraction layer over two lower-level drivers:./src/arch/shared/rawnetarch_unix.c(the libpcap-based driver --- maybe the file should be renamed...)src/arch/shared/rawnetarch_tuntap.c(the new TUN/TAP-based driver)The two drivers can be switched at run-time.
The patch also includes some refactoring: it moves some code and comments from
rawnetarch_unix.ctorawnetarch.c, to avoid duplication inrawnetarch_tuntap.c.I hope this makes sense. Questions and comments are welcome :-)
Last edit: Alceste Scalas 2021-01-02
Uh, I realised that something is missing in the patch, for Windows. The following new functions (declared in
rawnetarch.h) need to be defined inrawnetarch_win32.c:They should simply do nothing and return success.
applied the patch in r39893 (sorry for the delay) i verified ethernet still works for me with pcap, so at least that didnt break. i didnt test any of the new things.
one thing i didnt quite understand: why at the bottom of rawnetarch.c there is
?
also, some things are still missing to make this perfect:
Perhaps just remove the root check and also don't check for permissions, but just present/log a proper error message?
Great news! Unfortunately I will be very busy in the upcoming weeks, but I will try to update the documentation. Meanwhile, here are a few answers:
Good question! Those
#ifdefs are a leftover from the original patch, and they should be removed. Now bothrawnetarch_tuntap.candrawnetarch_unix.care always compiled under Unix (seeMakefile.am), and they have their own#ifdefs to check whether their dependencies are available.Suggestion: maybe it is worth adding the option
--warn-commonwhen generatinglibarchdep.aand theviceexecutables? If I am not mistaken, it should detect duplicated symbol definitions during static linking, and it would have helped in spotting the redundant#ifdefs above.Regarding the "is root" checks for libpcap, and the chained assignments pointed out by @compyx : I agree they should be improved, but I must confess that I copy'n'pasted both of them from the original code (in particular,
rawnetarch_unix.c) :-)Last edit: Alceste Scalas 2021-04-03
Next time, please don't do this:
Took me almost 30 seconds to figure out what was going on there. VICE isn't competing in the IOCCC, though some parts could I suppose ;)
But massive kudos for actually documenting the code, using Doyxgen even, that makes me very happy =)
Ok i cleaned up (removed) that ifdef, and removed the root check (which is really a bit questionable).
So the only major thing missing is the documentation now...
ok so, i renamed -cs8900ioif to -ethernetioif and -cs8900iodriver to -ethernetiodriver.
and i also updated/added docs for the ethernet related cmdline options and resources....
Missing now is:
Regarding the GUI support: I also wanted to only show TUN/TAP devices, but
For these reasons,
rawnet_arch_tuntap_enumadapter()(inrawnetarch_tuntap.c) lists all network devices in a way that should be portable across Unix variants. For the GUI, I would probably just add a helpful error message with a list of things to check in case of error:For a start, it would be fine with me to have a way to select the device at all.... one that isnt a textbox that is :)
Just for clarity, I am attaching the Ethernet settings window I can see on my system, running r39902.
This drop-down box allows users to select the TUN/TAP device, without having to write its name in a textbox: would this be enough, besides some polish and more helpful error messages?
Besides: I have noticed that vice is now sometimes trying to load the libpcap Ethernet driver first, and causing errors without falling back to the TUN/TAP driver (even if I select the latter in vicerc, via
ETHERNET_DRIVER="tuntap").I suspect this is related to the removal of root checks for libpcap in r39900, that subtly break my patch (which used root checks as found in the rest of the vice codebase to select which Ethernet driver to use).
If root checks are removed, then there should be some way to tell whether the libpcap driver is actually usable by the current user. Recovering from an error later might be much trickier...
Last edit: Alceste Scalas 2021-04-05
Thats just fine IMHO. Of course proper error messages are always a plus :)
r39904 hopefully fixes this... it now checks for root or CAP_NET_RAW. please test :)
r39915 also adds an enumerate function for the driver ... there are some more challenges left to make this stuff more sane:
please test! :)
Last edit: gpz 2021-04-10
I tested r39922, and it looks good to me. Thanks!
I am attaching a small patch that improves the error message when the Ethernet cartridge emulation cannot be initialized. The main use case the patch addresses is: a user enables Ethernet cartridge emulation without first setting up
ETHERNET_INTERFACE(either on.vicercor via the GUI). When this happens, VICE selects a default interface (eth0on Unix) that may not even exist, and this may be quite puzzling.applied, thanks
can we close this?
Yes, I think so. Thanks!