0 notes &
Running a process inside a network namespace
I have been reading and playing with Linux containers. I previously covered cgroups which enables process group level cpu/memory allocation. This blog entry is about my understanding of network namespace. And about running a process inside an isolated network namespace. Running a process there allows us to set specific filters on the specific process.
Network namespace allows Linux to clone the network stack and make the new stack available to a limited set of processes. This is used primarily with Linux containers such that each container has a different network stack altogether. There are multiple options for adding network interfaces to a newly created network namespace
- veth
- venet
- vlan
Out of the three options above, I haven’t been able to find a lot about venet. I think venet is part of OpenVZ kernel changes and is not available in mainstream linux kernel.
A new network namspace can be created using the following command
ip netns add myspace
Now, we will create a new pair of type veth network interfaces. veth interfaces come in pair and act like a pipe of data. Each packet sent to veth0 shows up at veth1 and each packet to veth1 shows up at veth0.
ip link add veth0 type veth peer name veth1
Now, let’s move veth1 to our newly created namespace
ip link set veth1 netns myspace
Now, we will bring up veth0 (in original namespace) and assign IP address and subnet to it
ifconfig veth0 192.168.42.1 netmask 255.255.255.0 up
Assigning ip address and netmask to veth0 (inside myspace namespace)
ip netns exec myspace ifconfig veth1 192.168.42.2 netmask 255.255.255.0 up
The command above is important to look at again. The format of the above command is ip netns exec myspace <command>. The <command> executed here will be running in the myspace network namespace. The command will only see the interfaces, the route table configured inside the myspace network namespace.
Setting up gateway for myspace namespace
ip netns exec myspace route add default gw 192.168.42.1
Now, we have a namespace myspace which can send network packets to veth0 which will reach host at veth1 interface. Here, we can multiple options to connect veth1 to the outside world (via eth0).
- Bridging
- NAT
I will be covering NAT setup here. NAT can be enabled by running the following commands
# Enable kernel to forward packets from one interface to another (veth0 <-> eth0)
echo 1 > /proc/sys/net/ipv4/ip_forward
# Each packet coming via 192.168.42.* address space should be sent
# via eth0 after changing the source ip to eth0 ip.
iptables -t nat -A POSTROUTING -s 192.168.42.0/24 -o eth0 -j MASQUERADE
Now, our network namespace is ready for use. A process can be run inside the network namespace by using the following
ip netns exec myspace <command>
The command will run inside the network namespace. If you want to run the command inside network namespace as an unprivileged user, use the following
# first sudo is optional and only needed if running this command as non-root
sudo ip netns exec myspace sudo -u <user> <command>
This way, all the traffic generated by the process will be inside the network namespace myspace. The traffic can be inspected and accepted or dropped in the parent namespace by using the filter table, forward chain.