Networking and the Raspberry Pi

Some good references

MAC layer

The MAC layer (Medium Access Control) provides the lowest level the programmer and system administrator can access. The computer receives physical packets encoded in a format defined in the MAC layer definition. Here’s how an Ethernet packet might encapsulate data destined for a higher layer.
802.1Q packet

MAC addresses (also know as physical or hardware addresses) are 48 bits long and typically written as a 12-digit hexadecimal number where colons separate pairs of hexadecimal digits, for example 00:25:64:C0:AC:81.

Generally large ethernets are managed by spanning trees switches that learn the topology of the network and route packets to the appropriate segment of the net. They look like an ordinary Ethernet to the computer.
spanning tree
Everything you need to know about spanning trees can be learning from the poem Algorithm written by its inventor, Radia Perlman.

Like Ethernet, Wi-Fi is also a MAC-layer protocol.

MAC level administrative programs

IP layer

In the IP layer routers direct packets between physical networks. Thus the physical internet is a collection of local networks joined by routers. The Internet Protocol was specified by RFC 791 in 1981.

Here is the summary of an IP packet header from RPC 791. This is the begining of the payload for an Ethernet packet.

    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Version|  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                    Example Internet Datagram Header

The interface of every computer on the internet is identified by a unique 32-bit address written in dot-decimal notation, four 8-bit numbers separated by periods such as 152.18.69.35 . These addresses are assigned by allocating ranges of 32-bit addresses to organizations using CIDR (classless inter-domain routing) notation. For example, NC-REN manages 152.16.0.0/12, UNC Asheville manages 152.18.0.0/16, and the computer science department manages 152.18.69.0/24 .

IP to MAC address matching

Routers direct data to the target local network using the IP address. On the local network the router determines the MAC address of the target by using ARP (Address Resolution Protocol). This is accomplished by broadcasting the desired IP address to all computers on the local network. The one holding the targeted IP will reply with its MAC address which will then be stored in the ARP table so that the broadcast can be avoided when the next packet arrives.

IP level administrative tools

IPv6

Almost all IP traffic uses IP version 4. IPv6 was defined in 1995 and is specified in RFC 2460. It is a significant change to IPv4. For example, hosts have 128-bit rather than 32-bit addresses. It is claimed that there are some computers using IPv6 out there.

TCP layer

There are two well-used layers above IP, TCP (Transmission Control Protocol) and UDP (User Datagram Protocol). UDP is heavily used for media applications, like video, and for gaming. TCP is used for most everything else, such as email and web page retrieval.

TCP was specified by RFC 793 in 1981 but has had several performance related improvements in the last 32 years. TCP provides reliable stream-oriented connections between applications running on networked computers. These connections are identified by 16-bit port numbers. Most server port numbers are well known. For example, port 80 is for HTTP (web), port 22 is for SSH, port 25 is for SMTP (mail), and port 53 is for domain (IP name lookup). Client port numbers are usually generated by the operating system.

As you can imagine, TCP has a pretty complicated header.

    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                            TCP Header Format

TCP level administrative tools

Applications

On top of TCP and UDP there are many application protocols. Many of these are specified in very long documents, such as RFC 2821 for mail and RFC 2616 for the World Wide Web.

Network address translation

Network address translation is a scheme in which a router changes the TCP port number and IP address of packets within a TCP session.

For example, I am writing this page while connected, via ssh, from my home Linux system to oteen.cs.unca.edu. My home system thinks its IP address is 192.168.0.217 and that it is using port number 37905 to connect to port number 22 on 152.18.69.35, oteen.cs.unca.edu. However, oteen.cs.unca.edu thinks that its port 22 is connected to port 37905 at IP address 24.196.111.222, which is the IP address that Charter has assigned to my router. My router is changing the IP number in the IP header of packets that pass between my Linux computer and oteen. Sometimes it may even change the TCP port number because two computers in my house may have independently initiated a TCP connection using the same port number.

Domain name lookup

RFC 1035 describes a system for translating domain names, such as oteen.cs.unca.edu, into IP addresses by using a distributed network of servers. Compromizing one domain server can disable large portions of the internet as was the case in the recent denial-of-service attack on the New York Times web site.

Domain name system administrative tools

Getting you connected via the UNC Asheville student VLAN

Let’s hope this work. First remove the Wireless USB adapter from your Pi. Next check the file /etc/udev/rules.d/70-persistent-net.rules. If it contains two lines similar to the following, you must delete them and reboot your Pi.

# USB device 0x:0x (rtl8192cu)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="80:1f:02:9b:c8:dd", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

One file solution

In this method you change only one file by adding the five lines at the end. Only then may you plug in your wireless USB adapter.

Unfortunately, this isn’t working very well.

/etc/network/interfaces

auto lo

iface lo inet loopback
iface eth0 inet dhcp


auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
   wpa-ssid "csci"
   wpa-scan-ssid 1
   wpa-key-mgmt NONE

Two file solution

/etc/network/interfaces

auto lo

iface lo inet loopback
iface eth0 inet dhcp


auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
   pre-up wpa_supplicant -B -i wlan0 -Dwext -c /etc/wpa_supplicant/csci.conf
   post-down kill -q wpa_supplicant

/etc/wpa_supplicant/csci.conf

network={
    ssid="csci"
    key_mgmt=NONE
    scan_ssid=1
}