Skip to content

Commit

Permalink
Merge pull request lxc#395 from stgraber/network
Browse files Browse the repository at this point in the history
incusd/forknet: Handle wifi detach
  • Loading branch information
hallyn authored Jan 16, 2024
2 parents 212bc90 + 61a7f32 commit cf47ad6
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 additions & 5 deletions cmd/incusd/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,13 +122,16 @@ import (
"encoding/json"
"fmt"
"net"
"os"
"strings"

"github.com/spf13/cobra"

"github.com/lxc/incus/internal/netutils"
"github.com/lxc/incus/internal/server/ip"
_ "github.com/lxc/incus/shared/cgo" // Used by cgo
"github.com/lxc/incus/shared/subprocess"
"github.com/lxc/incus/shared/util"
)

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

// Check if the interface exists.
if !util.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 @@ -197,13 +221,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 @@ -216,10 +241,27 @@ func (c *cmdForknet) RunDetach(cmd *cobra.Command, args []string) error {
return err
}

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

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

return nil
Expand Down

0 comments on commit cf47ad6

Please sign in to comment.