Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

2 · The Linux network stack

In one sentence: Linux keeps networking in five tables (interfaces, addresses, routes, neighbours, sockets); a network namespace is a separate, empty copy of all five, and a veth pair is a cable between two of them.

The problem it solves

Lesson 1 split a connection into questions: which address, which way out, who on the link, is anyone listening. To check each one you need to know where Linux keeps the answer. And to practise breaking networks you need several machines you can wreck in seconds, without touching your own computer’s network.

Both have the same answer. The kernel’s networking lives in a few tables you can read with ip and ss, and Linux can make as many independent copies of those tables as you like.

Five tables

Here is the lab container you’ll build in Practical 0, read table by table. -br asks ip for its brief, one line per item form.

Interfaces (ip -br link): the places packets go in and out. A real network card, a virtual one, or lo, the loopback, which is the machine talking to itself.

lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP> 
eth0@if822       UP             a2:f4:40:4f:79:35 <BROADCAST,MULTICAST,UP,LOWER_UP> 

The columns are name, state and MAC address, the interface’s hardware address used on its own link. The flags in <...> follow. UP means you switched it on. LOWER_UP means the other side is there too, like a cable plugged in at both ends.

Addresses (ip -br addr): the IP addresses each interface answers to.

lo               UNKNOWN        127.0.0.1/8 ::1/128 
eth0@if822       UP             172.17.0.3/16 

The /16 after the address says how big the local network is (lesson 3 takes it apart).

Routes (ip route): for each destination, which interface to send it out of, and through which machine.

default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.3 

The second line says that anything in 172.17.x.x is on eth0’s link, so it goes straight there. Nobody typed it: proto kernel means the kernel added it when the address was added. The first line, default, catches everything else and hands it to 172.17.0.1, the gateway, a machine that forwards packets onwards.

Neighbours (ip neigh): the MAC addresses of machines on the same link that this one has talked to. After one ping to the gateway:

172.17.0.1 dev eth0 lladdr ea:92:21:b2:12:84 REACHABLE 

Sockets (ss -tln): programs waiting for TCP connections (-t TCP, -l listening, -n numbers, not names). Nothing listens in a fresh container, so it prints only its header:

State Recv-Q Send-Q Local Address:Port Peer Address:Port
TableAnswersRead it with
interfacesis the cable there and switched on?ip link
addresseswhich addresses are mine?ip addr
routeswhich way to that address?ip route
neighbourswhich MAC has that address on my link?ip neigh
socketsis a program listening on that port?ss -tln

A namespace: a whole new set of tables

ip netns add red creates a network namespace called red. It is a second network stack inside the same kernel, with its own five tables, and it starts almost empty. ip -n red <command> runs an ip command in it; ip netns exec red <program> runs any program there.

ip netns add red
ip -n red link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

Only a loopback, and it’s DOWN. No addresses, no routes (ip -n red route prints nothing), no sockets. Even talking to itself fails:

ip netns exec red ping -c1 127.0.0.1
ping: connect: Network is unreachable

That message means the stack has no route for the address. With lo down, 127.0.0.1 isn’t anywhere. ip -n red link set lo up fixes it.

A program started in a namespace sees only that namespace’s tables. A server listening on port 8080 in blue shows up in ip netns exec blue ss -tln and nowhere else. That is what makes a namespace behave like a separate machine, and it is exactly how Docker gives each container its own network.

A veth pair: a cable

A veth pair (virtual Ethernet) is two interfaces created together. A packet sent into one comes out of the other. Create the pair, then move each end into a namespace:

ip link add veth-red type veth peer name veth-blue
ip link set veth-red netns red
ip link set veth-blue netns blue

Before the move, ip -br link in the container shows both ends, each naming its partner after the @. After it, they’re gone from the container and appear in red and blue. Add an address on each end (ip -n red addr add 10.0.0.1/24 dev veth-red, and 10.0.0.2/24 in blue). A cable still needs both ends switched on. With only red’s end up:

lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP> 
veth-red@if3     LOWERLAYERDOWN 92:21:d1:d5:48:61 <NO-CARRIER,BROADCAST,MULTICAST,UP> 

Red’s end is UP, but there’s NO-CARRIER: nothing alive on the other end. Red’s route is there, marked linkdown, and a ping just gets no answer:

10.0.0.0/24 dev veth-red proto kernel scope link src 10.0.0.1 linkdown 

Bring blue’s end up too, and red can reach blue:

64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from 10.0.0.2: icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from 10.0.0.2: icmp_seq=3 ttl=64 time=0.037 ms

Red’s neighbour table now holds blue’s MAC, the same a2:ee:74:23:6c:42 that ip -n blue link shows for veth-blue:

10.0.0.2 dev veth-red lladdr a2:ee:74:23:6c:42 REACHABLE 

Try it on your computer

Practical 0 · Your lab: two namespaces and a cable: build the lab container, make red and blue, join them with a veth pair and ping across it. Then unplug it on purpose and read what each table says.

Go deeper: what a namespace is, and where it lives

The kernel has several kinds of namespace: network, mount, process IDs, users and others. Each gives a process its own view of one resource. A container is a process with several of them at once. A network namespace holds its own interfaces, routing tables, neighbour tables, firewall rules, sockets and its own net.* settings under /proc/sys/net (network_namespaces(7)).

ip netns add names a namespace by bind-mounting it to a file in /run/netns/. While that mount, or any process in the namespace, exists, the namespace stays alive (ip-netns(8)). Restart the lab container and the mounts are gone, but the files remain. ip netns list then prints red and blue after four lines of Error: Peer netns reference is invalid., and entering one fails:

setting the network namespace "red" failed: Invalid argument

So namespaces don’t survive a restart. Practical 0 has you put the setup in a script for that reason.

Go deeper: the veth pair is one object

The two ends of a veth pair belong together: delete either end and the kernel deletes both. After ip -n blue link del veth-blue, ip -n red -br link shows only lo, and red’s 10.0.0.0/24 route goes with it. The @if3 after veth-red is the index of its partner interface, which lives in another namespace, so ip can’t show its name. ip -n red link show veth-red names that namespace: link-netns blue.

Docker uses the same pieces. Each container gets a namespace and one end of a veth pair; @if822 in the container’s eth0@if822 is the interface number of the other end, which stays on the host. For a lab container whose eth0 said @if841, the host’s interface 841 was:

841: veth038851b@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT ...

That is a veth end, plugged (master docker0) into Docker’s bridge, a virtual switch that lesson 5 builds by hand.

Common misunderstandings

  • “A namespace is a container.” A container uses a network namespace, plus others for files and processes. A bare network namespace shares the container’s files and programs; only the network is separate.
  • “UP means it works.” UP means you switched your end on. LOWER_UP (or no NO-CARRIER) means the other end is there too. Read both.
  • “I have to add the route for my own network.” Adding 10.0.0.1/24 on an interface that’s up adds the 10.0.0.0/24 route by itself (proto kernel). You add routes only for networks you reach through another machine (lesson 4).

Check yourself

1. In a brand-new namespace, ping 127.0.0.1 says Network is unreachable. Which table, and what fixes it? The routes: with lo down, the stack has no route for 127.0.0.1. ip -n <ns> link set lo up brings it up, and loopback addresses work.
2. ip -n red -br link shows veth-red as LOWERLAYERDOWN with NO-CARRIER. What do you check next? The other end of the cable. Red's end is up, but its partner isn't: look in the namespace that holds it (ip -n blue -br link) and bring it up.
3. A server listens on port 8080 in blue. You run ss -tln in the container and don't see it. Is the server down? Not necessarily: ss shows the sockets of the namespace it runs in. Run ip netns exec blue ss -tln to see blue's.

Next: Practical 0 · Your lab, then lesson 3 · IP addresses and subnets.