Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable user-mode networking for unprivileged network namespaces #40

Open
jserv opened this issue Feb 18, 2024 · 2 comments
Open

Enable user-mode networking for unprivileged network namespaces #40

jserv opened this issue Feb 18, 2024 · 2 comments
Assignees

Comments

@jserv
Copy link
Collaborator

jserv commented Feb 18, 2024

Beginning with version 3.8 of Linux, users without privileged access have been able to create network_namespaces and user_namespaces. Despite this, the utility of unprivileged network namespaces was limited because the creation of virtual ethernet (veth) pairs, which bridge the host and network namespaces, still demanded root access, effectively barring internet connectivity.

The slirp4netns provides a solution to this limitation by enabling an unprivileged connection of a network namespace to the internet. It achieves this by linking a TAP device within a network namespace to a TCP/IP stack operating in user mode, known as slirp, thereby facilitating internet access without the need for root privileges. This means that it is feasible for users to integrate slirp4netns to run a RISC-V/Linux guest with full user-mode networking.

Reference:

@chiangkd
Copy link
Collaborator

chiangkd commented Nov 4, 2024

The slirp4netns utility connects a TAP device within a network namespace and sends packets to the host network stack through this device.

/* implements SlirpCb.send_packet */
static ssize_t libslirp_send_packet(const void *pkt, size_t pkt_len,
                                    void *opaque)
{
    struct libslirp_data *data = (struct libslirp_data *)opaque;
    return write(data->tapfd, pkt, pkt_len);
}

In the context of SEMU, to send packets to the host through a TAP device, I need to create a separate process (child) that uses unshare to establish a new network namespace. This can be done with the flags CLONE_NEWUSER | CLONE_NEWNET, while the kernel operations are managed by the parent process.

For reference, I examined the QEMU/slirp.c and libvdeslirp library. Functions like slirp_add_hostfwd and slirp_add_guestfwd are used to create port forwarding rules.

Is it possible to forward traffic from eth0 in the VM to the host machine?

@jserv
Copy link
Collaborator Author

jserv commented Nov 4, 2024

In the context of SEMU, to send packets to the host through a TAP device, I need to create a separate process (child) that uses unshare to establish a new network namespace. This can be done with the flags CLONE_NEWUSER | CLONE_NEWNET, while the kernel operations are managed by the parent process.

You can send a pull request to consolidate the existing TAP based network device. Then, move forward to slirp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants