12
# ip link set  wlan0 netns 1
RTNETLINK answers: Invalid argument

It works for usual ethernet. It also works for proprietary broadcom "wl" driver.

How to do it for usual mac80211-based driver?

Vi.
  • 17,755

3 Answers3

15

You need to move the PHY:

iw phy phy0 set netns 666

Where 666 is the pid of some process running in that network namespace; for netspaces created by iproute2 tools (with ip netns), you can create a short-lived process in there and get its pid:

iw phy phy0 set netns "$(ip netns exec mynetns sh -c '
  sleep 1 >&- & echo "$!"')"

To move it back to the root namespace:

ip netns exec mynetns iw phy phy0 set netns 1
sch
  • 367
1

For wifi get the physical address of your interface first (in case your have more than 1 wireless card):

iw dev

The result will be somehting like this:

phy#0
        Interface wlp58s0
                ifindex 3
                wdev 0x1
                ...

So the name is phy0

now use that to assign your wifi to the namespace:

# iw phy phy0 set netns name <MyNamespace>

From the help of iw:

    phy <phyname> set netns { <pid> | name <nsname> }
            Put this wireless device into a different network namespace:
                <pid>    - change network namespace by process id
                <nsname> - change network namespace by name from /var/run/netns
                           or by absolute path (man ip-netns)
0

Implemented my own workaround: vethify

  1. Create a pair of virtual interfaces, move one of them (for example, veth1) into other namespace: ip link add type veth; ip link set veth1 netns <some_pid>;
  2. Bring wlan0 interface and veth0 up, but don't add any addresses to it;
  3. Start vethify wlan0 veth0 on host network namespace. It will copy all captured on wlan0 to veth0 and back;
  4. Configure IP address and routing in the other namespace on veth1;
  5. Disable checksum offloading for veth1: ethtool --offload veth1 rx off tx off && ethtool -K veth1 gso off.

Note: vethify sees all packets in the network namespace, not only wlan0's and veth0's. It will add latency and cause additional overhead.

Vi.
  • 17,755