How to move wireless connection to other network namespace?

8

1

# 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.

Posted 2013-10-03T17:26:24.420

Reputation: 13 705

Answers

11

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

Lubomir Rinteł

Posted 2013-10-03T17:26:24.420

Reputation: 126

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 – 2015-04-14T15:40:54.633

Crashed the kernel playing with iw ... setns... – Vi. – 2015-04-15T13:22:03.953

What is the 666? Man says it is a pid, but a pid of which process should I provide here? – Bjarke Freund-Hansen – 2016-09-12T07:44:13.737

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 – 2019-02-22T16:41:12.430

$ 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 – 2019-04-09T23:31:24.537

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.

Posted 2013-10-03T17:26:24.420

Reputation: 13 705