Salta al contenuto
Note per Studenti Emulating Networks with Katharà Report

Emulating Networks with Katharà

In questa pagina 9

See also: ARP - Address Resolution ProtocolTo place an IP datagram in a frame a node needs the MAC address of the next hop, obtained with ARP: a broadcast request (destination MAC FF:FF:FF:FF:FF:FF, carrying the sender's IP and MAC and the wanted IP) and a unicast reply with the wanted MAC, stored in the ARP cache; ARP always resolves the next hop (destination host or router), never the final destination, and since broadcasts do not leave the subnet a router can answer with its own MAC (proxy ARP).ARP - Address Resolution Protocol →, ICMP, Ping and TracerouteICMP adds to IP the error reporting and queries it lacks; its messages travel inside IP datagrams with Protocol 1 and are either errors (destination unreachable type 3, time exceeded 11, redirect 5, parameter problem 12), always sent to the original source with the original IP header plus 8 bytes of data, or queries (echo request 8 and reply 0, timestamp 13 and 14); ICMP only reports and never corrects errors, and it supports ping (RTT), path MTU discovery with the D bit (maximum payload 1500-20-8=1472 bytes) and traceroute (TTL increasing, n+1 probes for n routers), but also attacks such as smurf and fake redirects.ICMP, Ping and Traceroute →, Network Layer and IPv4 AddressingThe network layer delivers datagrams host to host through routers (encapsulation, routing, forwarding); an IPv4 address is 32 bits split into a network prefix and a host suffix, and modern classless addressing (CIDR) gives a block of 2^(32-n) addresses from a.b.c.d/n, with network and broadcast addresses computed through the netmask.Network Layer and IPv4 Addressing →.

Why an emulator

Real networks have many devices, interfaces and protocols, and experimenting on a company or university network is impossible; buying equipment is costly. The solution is a network emulator.

Definition (simulation and emulation). A simulator reproduces the performance of a real system (delays, losses) with a model. An emulator reproduces the functions (configurations, architectures, protocols): the programs that run are the real ones, with little attention to performance.

Example. A ping between two Katharà containers answers in under 1 ms because they are on the same machine: times are not those of a real link; in the lab one checks whether packets arrive and which way they go.

Katharà (Kathará, the successor of Netkit, built with Python and Docker) is the emulator used in the lab. Every emulated device (host, router, server) is a container, created from an image: Base (DNS, web server, network tools), Quagga and FRRouting (standard routing protocols; used for RIP with kathara/frr, see RIP and Advanced Routing in KatharàLab 3 configures static routes on a three-router triangle with /20 and /30 networks so that every interface answers every ping, and uses traceroute to discover routers through the TTL; Lab 5 replaces manual tables with RIPv2 run by FRRouting (zebra and ripd, vtysh, router rip, network, redistribute connected), on a five-router stub network with a default route injected into RIP.RIP and Advanced Routing in Katharà →), Open vSwitch, Behavioral Model (P4).

Definition (device and collision domain). Each device has a console (terminal window), memory, its own filesystem and zero or more network interfaces (eth0, eth1, ...). Each interface connects to a virtual collision domain, named by a letter or a name (A, B, MYNET), which can connect several interfaces.

Example. If pc1.eth0 and pc2.eth0 are on domain A, the two hosts are on the same LAN as if attached to the same hub. So in Katharà no hubs or switches are added. A collision domain corresponds to an IP network; devices in different domains communicate only through a router (Static Routing and Diagnostic Tools in KatharàLab 2 builds by hand the routing tables of two LANs (195.11.14.0/24 and 200.1.1.0/24) joined by routers r1 and r2 over 100.0.0.8/30: an active interface adds only its directly connected network, hosts use a default route, routers need static routes in both directions, and ping, route -n, tcpdump and traceroute diagnose the result (ttl=62 after two routers).Static Routing and Diagnostic Tools in Katharà →).

Katharà commands

family command what it does
v-commands (one machine) vstart starts a new device with chosen interfaces
vconfig attaches more interfaces to a running device
vclean stops a device
l-commands (a whole lab) lstart starts all devices of the lab
lclean stops all devices
lrestart stops and restarts (resets e.g. ARP caches)
linfo lab information
lcrash stops devices abruptly
global check checks the environment
connect opens the console of a running machine
list lists running machines
settings shows and changes settings
wipe deletes all machines and links
bash
kathara check                          # test the installation
kathara vstart -n pc1 --eth 0:A        # start pc1 with eth0 on domain A
kathara vclean -n pc1                  # stop pc1

In the university lab the host terminal prepares the environment with source ./kathara.sh, then cd into the lab folder and lstart / lclean; lwipe removes every leftover (machines and virtual networks).

Sharing files with the real machine

The /shared folder inside each device points to the lab's shared folder (enabled by default, read and write). /hosthome points to the real user's home (disabled by default, enabled with kathara settings). Example. tcpdump -i eth0 -w /hosthome/capture.pcap puts the capture in the user's home, outside the container.

Structure of a lab

Definition (Katharà lab). A folder containing a lab.conf file with the topology; a <name>.startup file per device (commands run inside the device right after boot); optionally a subfolder <name>/ per device, whose content is copied into the root / of that device's filesystem.

Example. pc1/foo/file.txt becomes /foo/file.txt in pc1; vpc/var/www/index.html puts a web page in server vpc; r1/etc/shadow sets the root password of r1.

lab.conf has lines machine[argument]=value: if the argument is a number, value is the collision domain of interface eth<number>; otherwise the argument is an option name.

pc1[0]=A
pc2[0]=A
pc2[1]=B
pc3[0]=B

pc1 has eth0 on A; pc2 has eth0 on A and eth1 on B (so it can act as a router); pc3 has eth0 on B.

line meaning
r1[image]="kathara/frr" container image (FRR daemons)
s1_client[num_terms]=1 number of terminal windows (0 = none)
s2_attacker[bridged]=1 also connect to the physical network
s2_client[sysctl]="net.ipv4.conf.all.arp_accept=1" set a kernel parameter in the container
s1_client[ipv6]=0 disable IPv6

.startup files are shell scripts run inside the device at boot, typically configuring interfaces and starting services:

bash
ifconfig eth0 10.0.0.1/24 up        # assign the address and activate the interface
/etc/init.d/zebra start             # start a service (here a routing daemon)

Configuring an interface with ifconfig

Formula. ifconfig <interface> <address> netmask <mask> broadcast <broadcast address> [mtu <bytes>] up, equivalent to ifconfig eth0 <address>/<length> up.

Example. ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up equals ifconfig eth0 10.0.0.1/24 up.

  • netmask 255.255.255.0: first 2424 bits are the network part. Network address = address AND mask: 10.0.0.1 AND 255.255.255.0 == 10.0.0.0. Broadcast = network OR (NOT mask) == 10.0.0.255. There are 28=2562^{8}=256 addresses, 254254 for hosts. Without the mask Linux uses the class mask, wrong for subnetted networks (e.g. /30): always write it.
  • up activates the interface; Linux then automatically adds the directly connected network (10.0.0.0/24 on eth0) to the routing table, which is why two hosts on the same network reach each other without further configuration.

The MTU can be changed: ifconfig eth0 10.0.0.5 mtu 4000 up (address 10.0.0.5, MTU 40004000 bytes; the default mask for 10.x.x.x is class A, 255.0.0.0255.0.0.0, so better to specify it).

The "Two Hosts" lab

pc1 10.0.0.1/24 and pc2 10.0.0.2/24 on domain A.

Step 1: create the machines.

bash
kathara vstart -n pc1 --eth 0:A
kathara vstart -n pc2 --eth 0:A

The message network with name netkit_nt_A already exists is not a real error: domain A was created by pc1.

Step 2: configure the interfaces.

bash
pc1:~# ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up
pc2:~# ifconfig eth0 10.0.0.2 netmask 255.255.255.0 broadcast 10.0.0.255 up

Step 3: ping.

bash
pc1:~# ping 10.0.0.2
64 bytes from 10.0.0.2: icmp_seq=1 ttl=64 time=2.65 ms
5 packets transmitted, 5 received, 0% packet loss

ping sends ICMP echo request messages and waits for echo reply. 56(84) bytes: 5656 bytes of data +8+8 ICMP +20+20 IP =84=84 bytes. icmp_seq: request number. ttl=64: stays 6464 because there is no router between the hosts; each router subtracts 11. time: RTT; the first is higher because it includes ARP resolution. Options: -c N (N requests), -w s (maximum wait); stop with Ctrl+C.

Step 4: tcpdump is a packet sniffer:

bash
pc2:~# tcpdump -i eth0
19:27:17.899782 arp who-has 10.0.0.2 tell 10.0.0.1
19:27:18.002578 arp reply 10.0.0.2 is-at fe:fd:0a:00:00:02
19:27:18.004384 IP 10.0.0.1 > 10.0.0.2: icmp 64: echo request seq 1
19:27:18.005806 IP 10.0.0.2 > 10.0.0.1: icmp 64: echo reply seq 1
  1. ARP request (who-has 10.0.0.2 tell 10.0.0.1): pc1 knows only the IP of pc2 but needs its MAC for the Ethernet frame, so it asks in broadcast;
  2. ARP reply (is-at ...): pc2 answers in unicast;
  3. echo request and echo reply, one pair per icmp_seq.

ARP appears only the first time (then the cache has the MAC).

option meaning
-i eth0 interface to listen on
-w file.pcap save packets to a file
-r file.pcap read a saved file
-n / -nn no name resolution / also no protocol and port names
-t no timestamps (not meaningful in Katharà)
-e show the link-layer header (MAC addresses)
-c N stop after NN packets

tcpdump -tenni eth0 combines -t -e -n -n -i.

Step 5: graphical view. Save with tcpdump -i eth0 -w /hosthome/capture.pcap and open the file on the real computer with Wireshark, a packet dissector showing every field of every header and the payload.

Katharà MAC addresses

MACs have the form fe:fd followed by the four bytes of the IP in hexadecimal: 10.0.0.2 →\to fe:fd:0a:00:00:02 (10=0a10=\text{0a}); 200.1.1.7 →\to FE:FD:C8:01:01:07 (200=12⋅16+8=C8200=12\cdot16+8=\text{C8}); 195.11.14.5 →\to FE:FD:C3:0B:0E:05. This is the emulator's choice; in a real network the MAC is assigned by the manufacturer.

Lab exercises

  • Ping to a non-existing address. In the same network (e.g. 10.0.0.99): nobody answers the ARP who-has, ping prints Destination Host Unreachable. Outside the network without route or default gateway (e.g. 192.168.5.5): connect: Network is unreachable immediately, nothing is sent. Reason: the only table entry is 10.0.0.0/24; 192.168.5.5 AND 255.255.255.0 == 192.168.5.0, no match. A wrong mask causes the same: with pc1 at 10.0.0.1/25 and pc2 at 10.0.0.200, 10.0.0.200 AND 255.255.255.128 == 10.0.0.128, not the network of pc1.
  • ARP in Wireshark. The request has source MAC of pc1 and destination ff:ff:ff:ff:ff:ff; the reply carries the MAC of pc2.
  • Different domains. pc3 and pc4 on another domain B are unreachable from pc1: no router. In another IP network ping gives Network is unreachable; in the same IP network ARP would not cross the domain border.

Common mistakes

  • Forgetting up: the interface stays down and is absent from the routing table.
  • Wrong mask: different networks on the same domain, hence Network is unreachable.
  • Machines on different domains without a router.
  • Judging performance from ping times: Katharà emulates functions, not delays.
  • Forgetting lclean and lwipe.

Versione ripasso

bash
kathara check
kathara vstart -n pc1 --eth 0:A    # pc1 with eth0 on domain A
kathara vclean -n pc1
  • Files. /shared is the lab folder (default); /hosthome the user's home (enable with kathara settings). tcpdump -i eth0 -w /hosthome/capture.pcap.
  • Lab. lab.conf (topology), <name>.startup (boot script per machine), <name>/ folder copied into / (pc1/foo/file.txt becomes /foo/file.txt).
  • lab.conf. machine[N]=domain puts ethN on a domain; non-numeric arguments are options: image, num_terms, bridged, sysctl, ipv6. Example: pc1[0]=A, pc2[0]=A, pc2[1]=B, pc3[0]=B (pc2 in the middle can route).
  • ifconfig. ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 [mtu N] up == ifconfig eth0 10.0.0.1/24 up. Always write the mask. up adds the directly connected network to the routing table. MTU: ifconfig eth0 10.0.0.5 mtu 4000 up.
bash
pc1:~# ifconfig eth0 10.0.0.1 netmask 255.255.255.0 broadcast 10.0.0.255 up
pc1:~# ping 10.0.0.2
pc2:~# tcpdump -tenni eth0

Teoria collegata