Close

Tap/Tun Overview

A project log for $0.87 WiFi for Raspberry Pi and Arduino Projects

My project goal is to make internet access for micro controllers dirt cheap and dead simple to implement in your own projects.

surfingstevesurfingsteve 08/20/2014 at 06:520 Comments

Central to this project is using a Raspberry Pi, or similar linux device, to act as a bridge between the internet and the nRF24L01+ device.  Our task includes two steps.  First we create a network driver for the nRF24L01+.  Second we bridge this new device we created with the ethernet port on the Raspberry Pi.  Wikipedia: Network Bridging  Alternatively we could setup Routing, IP Masquerading (NAT) or any other linux network techniques to our newly created interface, but that discussion is beyond the scope of this hack.

Building a network device driver in linux requires kernel device programming (not as hard as it sounds) and constant updates with each new kernel release.  This can be a lot of work and a pain to maintain your code. Fortunately there is a way in software to create a virtual network interface that can be initialized by a simple C program.  This is a trick often used by VPN clients in linux to create virtual network interfaces to a remote network.

There are two types of virtual network interfaces in Linux, TAP and TUN.  Paraphrasing Wikipedia, both devices are purely created in software.  TUN, short for tunnel, simulates a layer 3 IP communications, think IP addresses.  TAP, short for network TAP, simulates layer 2 data link communications, think MAC addresses.  Now might be a good time to review the network Wikipedia: OSI layer model.  

For our project we are going to use the TAP interface.  This allows us to operate at the layer 2 level and forward ethernet data in a more raw form.  We just have packets of bytes we want to shuttle back and forth with no concept in our software of IP addresses, IPv4/IPv6 or TCP/UDP.  These are all things we can support without having to know anything about then.  This is an advantage of the network OSI model.  This gives us great flexibility of being protocol agnostic.  Our nodes can use DHCP, send network broadcasts, or communicate in IPv6.  Our hardware doesn't know or care because it's all bytes to us at the layer 2 level.  As a side note the RF modulation would be the layer 1 level in this use case, but for now that's a detail we can safely ignore.

Here is a quick example of a tap interface on a linux machine:

Discussions