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.
  • 16,755
  • 32
  • 111
  • 189

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
  • 347
  • 2
  • 8
Lubomir Rinteł
  • 166
  • 1
  • 3
  • Welcome to Super User! While this may answer the question, it would be a better answer if you could provide some explanation **why** it does so. – DavidPostill Apr 14 '15 at 15:40
  • Crashed the kernel playing with `iw ... setns`... – Vi. Apr 15 '15 at 13:22
  • What is the 666? Man says it is a pid, but a pid of which process should I provide here? – Bjarke Freund-Hansen Sep 12 '16 at 07:44
  • `666` is a PID of a process already in the network namespace you want to use. You can replace `666` by `name XXX` where `XXX` is the namespace's name. – Pierre Feb 22 '19 at 16:41
  • 1
    `$ sudo iw phy phy0 set netns 19470` `command failed: Operation not supported (-95)` (FFS, I've edited this comment so many times but nothing is working; how do I do this on multiple lines?) – flarn2006 Apr 09 '19 at 23:31
  • 1
    @flarn2006 the driver has to support it. A specific property must exist: `iw phy phy0 info | grep set_wiphy_netns` – A.B Jul 04 '20 at 21:48
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)
Fahad Alduraibi
  • 166
  • 1
  • 7
  • This answer repeats the accepted answer for the most part. You may want to edit the accepted include information about how to find `phy` instead of posting a semi-redundant new answer. – Vi. Aug 31 '22 at 11:58
  • 1
    His answer uses process ID, while there is an easier way to just use the name of the namespace. So his answer is still correct but I wanted to show the other possible way. – Fahad Alduraibi Aug 31 '22 at 13:54
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.
  • 16,755
  • 32
  • 111
  • 189