Skip to content

Commit

Permalink
lxd/forknet: Handle wifi detach
Browse files Browse the repository at this point in the history
Closes lxc/incus#385

Signed-off-by: Stéphane Graber <[email protected]>
(cherry picked from commit 61a7f3237527ac8736ed3267c8e237da4174ed34)
Signed-off-by: Kadin Sayani <[email protected]>
License: Apache-2.0
  • Loading branch information
stgraber authored and kadinsayani committed Sep 26, 2024
1 parent cb8a515 commit 14b8d71
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions lxd/main_forknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,27 @@ static int dosetns_file(char *file, char *nstype)
}
static void forkdonetdetach(char *file) {
// Attach to the network namespace.
if (dosetns_file(file, "net") < 0) {
fprintf(stderr, "Failed setns to container network namespace: %s\n", strerror(errno));
_exit(1);
}
if (unshare(CLONE_NEWNS) < 0) {
fprintf(stderr, "Failed to create new mount namespace: %s\n", strerror(errno));
_exit(1);
}
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) < 0) {
fprintf(stderr, "Failed to mark / private: %s\n", strerror(errno));
_exit(1);
}
if (mount("sysfs", "/sys", "sysfs", 0, NULL) < 0) {
fprintf(stderr, "Failed mounting new sysfs: %s\n", strerror(errno));
_exit(1);
}
// Jump back to Go for the rest
}
Expand Down Expand Up @@ -106,6 +122,8 @@ import (
"encoding/json"
"fmt"
"net"
"os"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -189,6 +207,11 @@ func (c *cmdForknet) RunDetach(cmd *cobra.Command, args []string) error {
return fmt.Errorf("hostname argument is required")
}

// Check if the interface exists.
if !shared.PathExists(fmt.Sprintf("/sys/class/net/%s", ifName)) {
return fmt.Errorf("Couldn't restore host interface %q as container interface %q couldn't be found", hostName, ifName)
}

// Remove all IP addresses from interface before moving to parent netns.
// This is to avoid any container address config leaking into host.
addr := &ip.Addr{
Expand All @@ -200,13 +223,14 @@ func (c *cmdForknet) RunDetach(cmd *cobra.Command, args []string) error {
return err
}

// Set interface down, rename it, and move into parent netns.
// Set interface down.
link := &ip.Link{Name: ifName}
err = link.SetDown()
if err != nil {
return err
}

// Rename it back to the host name.
err = link.SetName(hostName)
if err != nil {
// If the interface has an altname that matches the target name, this can prevent rename of the
Expand All @@ -219,10 +243,27 @@ func (c *cmdForknet) RunDetach(cmd *cobra.Command, args []string) error {
return err
}

link = &ip.Link{Name: hostName}
err = link.SetNetns(lxdPID)
if err != nil {
return err
// Move it back to the host.
phyPath := fmt.Sprintf("/sys/class/net/%s/phy80211/name", hostName)
if shared.PathExists(phyPath) {
// Get the phy name.
phyName, err := os.ReadFile(phyPath)
if err != nil {
return err
}

// Wifi cards (move the phy instead).
_, err = shared.RunCommand("iw", "phy", strings.TrimSpace(string(phyName)), "set", "netns", lxdPID)
if err != nil {
return err
}
} else {
// Regular NICs.
link = &ip.Link{Name: hostName}
err = link.SetNetns(lxdPID)
if err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 14b8d71

Please sign in to comment.