Thursday, May 3, 2012

Setup static ARP entry when using the tunnel device to communicate with each other over the GNURadio OFDM Link



---------- Forwarded message ----------
From: Alex Zhang <cingular.alex@gmail.com>
Date: Thu, May 3, 2012 at 11:30 PM
Subject: Setup static ARP entry when using the tunnel device to communicate with each other over the GNURadio OFDM Link
To: riveridea_wordpress <zuxo596ruhe@post.wordpress.com>


Here I am talking how to set the static routing entry between two nodes on both of which the tunnel device is used. As the physical layer is OFDM link which is sometime not so stable, the dynamic ARP query could degrade the link quality. The static ARP routing query is used in such experiment, with diagram of below.

     -----Socket-----TUN/TAP------OFDM-----> ======Radio===== ---->OFDM-----TUN/TAP------Socket--------
     \____________________________________/                   \_______________________________________/
                     Node 1                                                   Node 2


1. When creating the tunnel device, the MAC address of the tunnel device is autogenerated if not specified by yourself. And this MAC address is different for each time of creation of the tunnel device. But you can assign the fixed MAC address to the tunnel device by the iocntl() with SIOCSIFHWADDR option. Below is an example for such operation using Python code.

import os, sys
import random, time, struct
import fcntl
import socket
# /////////////////////////////////////////////////////////////////////////////
#
#   Use the Universal TUN/TAP device driver to move packets to/from kernel
#
#   See /usr/src/linux/Documentation/networking/tuntap.txt
#
# /////////////////////////////////////////////////////////////////////////////

# Linux specific...
# TUNSETIFF ifr flags from <linux/tun_if.h>

IFF_TUN        = 0x0001   # tunnel IP packets
IFF_TAP        = 0x0002   # tunnel ethernet frames
IFF_NO_PI    = 0x1000   # don't pass extra packet info
IFF_ONE_QUEUE    = 0x2000   # beats me ;)


# From linux/sockios.h
SIOCGIFINDEX = 0x8933
SIOCGIFFLAGS =  0x8913
SIOCSIFFLAGS =  0x8914
SIOCGIFHWADDR = 0x8927
SIOCSIFHWADDR = 0x8924
SIOCGIFADDR = 0x8915
SIOCSIFADDR = 0x8916
SIOCGIFNETMASK = 0x891B
SIOCSIFNETMASK = 0x891C
SIOCETHTOOL = 0x8946
 
# From linux/if.h
IFF_UP       = 0x1
 
# From linux/socket.h
AF_UNIX      = 1
AF_INET      = 2

def open_tun_interface(tun_device_filename):
    from fcntl import ioctl
   
    mode = IFF_TAP | IFF_NO_PI
    TUNSETIFF = 0x400454ca
   
    tun = os.open(tun_device_filename, os.O_RDWR)
    ifs = fcntl.ioctl(tun, TUNSETIFF, struct.pack("16sH", "gr%d", mode))

    ifname = ifs[:16].strip("\x00")

    newmac = '22:22:11:11:11:11'
    macbytes = [int(i, 16) for i in newmac.split(':')]
    ifreq = struct.pack('16sH6B8x', ifname, AF_UNIX, *macbytes)
    fcntl.ioctl(tun, SIOCSIFHWADDR, ifreq)

    ''' Obtain the device's mac address. '''
    ifreq = struct.pack('16sH14s', ifname, AF_UNIX, '\x00'*14)
    res = fcntl.ioctl(tun, SIOCGIFHWADDR, ifreq)
    address = struct.unpack('16sH14s', res)[2]
    mac = struct.unpack('6B8x', address)
    #print mac
 
    #print ":".join(['%02X' % i for i in mac])

    return (tun, ifname)

2. Add static ARP entry for the destination which is also a tunnel device
    As the tunnel device is now with a fixed MAC address for the destination, the source host can setup the static ARP entry as below:
    In /etc/network/if-up.d/, you can create a file named add-my-static-arp with below content:
    "arp -i gr0 -s 192.168.200.1 22:22:11:11:11:11"
    Every time, after you boot up the tunnel device in your source host, you can run the command of below to add such entry:
    /etc/network/if-up.d$ sudo ./add-my-static-arp

3. Of cours, you can not forget to config the IP address of your tunnel device as:
    sudo ifconfig gr0 192.168.200.2
   Before you config the IP address, you can ifconfig to check the name of your tunnel device.
  


--

Alex,

Dreams can come true – just believe.




--

Alex,
Dreams can come true – just believe.