Pages

Sunday, October 2, 2011

Make Your Ubuntu Box as your Personal WiFi Hotspot / Wifi Router



First of all, we have to be sure weather the wifi card supports host mode or master mode. While most of the wifi module support this , there is no guaranty that every card will have this feature.

To be sure, we can install iw package to check the wifi card does have master / host mode or not.

$ sudo apt-get install iw


Typing the above command in a terminal will install iw package. Now type the following in the terminal.

$ iw list

This shows the a full technical details of your wifi card. A sample output of the command is as follows.

Wiphy phy0
    Band 1:
        Capabilities: 0x11ce
            HT20/HT40
            SM Power Save disabled
            RX HT40 SGI
            TX STBC
            RX STBC 1-stream
            Max AMSDU length: 3839 bytes
            DSSS/CCK HT40
        Maximum RX AMPDU length 65535 bytes (exponent: 0x003)
        Minimum RX AMPDU time spacing: 8 usec (0x06)
        HT TX/RX MCS rate indexes supported: 0-15
        Frequencies:
            * 2412 MHz [1] (15.0 dBm)
            * 2417 MHz [2] (15.0 dBm)
            * 2422 MHz [3] (15.0 dBm)
            * 2427 MHz [4] (15.0 dBm)
            * 2432 MHz [5] (15.0 dBm)
            * 2437 MHz [6] (15.0 dBm)
            * 2442 MHz [7] (15.0 dBm)
            * 2447 MHz [8] (15.0 dBm)
            * 2452 MHz [9] (15.0 dBm)
            * 2457 MHz [10] (15.0 dBm)
            * 2462 MHz [11] (15.0 dBm)
            * 2467 MHz [12] (15.0 dBm) (passive scanning)
            * 2472 MHz [13] (15.0 dBm) (passive scanning)
            * 2484 MHz [14] (15.0 dBm) (passive scanning)
        Bitrates (non-HT):
            * 1.0 Mbps
            * 2.0 Mbps (short preamble supported)
            * 5.5 Mbps (short preamble supported)
            * 11.0 Mbps (short preamble supported)
            * 6.0 Mbps
            * 9.0 Mbps
            * 12.0 Mbps
            * 18.0 Mbps
            * 24.0 Mbps
            * 36.0 Mbps
            * 48.0 Mbps
            * 54.0 Mbps
    max # scan SSIDs: 4
    max scan IEs length: 2257 bytes
    Coverage class: 0 (up to 0m)
    Supported Ciphers:
        * WEP40 (00-0f-ac:1)
        * WEP104 (00-0f-ac:5)
        * TKIP (00-0f-ac:2)
        * CCMP (00-0f-ac:4)
        * CMAC (00-0f-ac:6)
    Available Antennas: TX 0x3 RX 0x3
    Configured Antennas: TX 0x3 RX 0x3
    Supported interface modes:
         * IBSS
         * managed
         * AP
         * AP/VLAN
         * WDS
         * monitor
         * mesh point
         * P2P-client
         * P2P-GO
    software interface modes (can always be added):
         * AP/VLAN
         * monitor
    valid interface combinations:
         * #{ managed, WDS, P2P-client } <= 2048, #{ AP, mesh point, P2P-GO } <= 8,
           total <= 2048, #channels <= 1
    Supported commands:
         * new_interface
         * set_interface
         * new_key
         * new_beacon
         * new_station
         * new_mpath
         * set_mesh_params
         * set_bss
         * authenticate
         * associate
         * deauthenticate
         * disassociate
         * join_ibss
         * join_mesh
         * remain_on_channel
         * set_tx_bitrate_mask
         * action
         * frame_wait_cancel
         * set_wiphy_netns
         * set_channel
         * set_wds_peer

Now look at the section "Supported interface mode" . If the list contains AP or AP/VLAN , then your wifi card supports master mode or host mode and you can proceed with this guide.

Step 1:
We will use hostapd package to use the wifi card as router /access point.


$ sudo apt-get install hostapd

After installing hostapd, we have to create or edit the hostapd configuration file which is located at /etc/hostapd/hostapd.conf .

$ sudo gedit   /etc/hostapd/hostapd.conf

Comment out all the existing entries and add the following lines.

interface=wlan0
driver=nl80211
ssid=your_ssid_name
hw_mode=g
auth_algs=1
wpa=2
wpa_passphrase=your_pass_phrase
wpa_pairwise=TKIP CCMP
wpa_key_mgmt=WPA-PSK
channel=1
rsn_pairwise=CCMP

Step 2:
We need to install a dhcp server and configure it for wlan0. I have noticed the default dhcp server (dhcp3) has issue with hostapd. So I just uninstalled the default dhcp client and installed dnsmasq . This is really awesome application. dnsmasq works as a server as well as a client. It is also very easy to configure.

$ sudo apt-get purge dhcpd  
$ sudo apt-get install dnsmasq

Now we will edit the default dnsmasq parameters in /etc/default/dnsmasq .

$ sudo gedit /etc/default/dnsmasq

The file should only contain the following.


DNSMASQ_OPTS=" -conf-file=/etc/dnsmasq.conf"

Save it . Now we will create a config file  /etc/dnsmasq.conf so that it gives the ip address to clients which tries to connect to hosted wifi access point.

$ sudo gedit /etc/dnsmasq.conf

Comment out all the existing entries add the following at the end of the file .

interface=wlan0
dhcp-range=192.168.0.2,192.168.0.127,12h

Save it.

Step3:

We have now setted up every thing to make our ubuntu box as wifi access point / wifi router.

Now we will create an executable script  say /usr/bin/wifi_ap so that we can run it as a command.
Add the following lines in /usr/bin/wifi_ap .

#!/bin/bash
# broadcasting interface
BROADCAST="wlan0"
BROADCASTIP="192.168.0.1"

# receiving interface broadcast is connected to. 
# put the correct interface name here. It is the network interface  
# which is connected to internet and whose connection is going to be 
#shared over wifi. E.g  for internal 3g slot it is "hso0" , for ethernet it is
#"eth0", for ppp it is "ppp0"
#For me it is an internal 3g slot .

RECEIVE="hso0"

if [[ $1 == "-0" || $1 == "--start" ]]
   then
      ## start hostapd

      echo "Starting hostapd"
      echo "You can view the log at /var/log/hostapd.log"
      sleep 1
 
      # launch hostapd daemon
 
      hostapd -d /etc/hostapd/hostapd.conf > /var/log/hostapd.log &
      echo "hostapd started.."
 
      ## start dhcp server; We are making sure that currently dnsmasq is not 
      #running by any other application. So before starting dnsmasq, we are 
      #trying to stop any previous session.

      echo "Stopping previous dhcp session"
      /etc/init.d/dnsmasq stop
      killall -2 dnsmasq
 
      echo "Starting dnsmasq DHCP Server on wlan0"
 
      # set IP address

      ifconfig $BROADCAST $BROADCASTIP
      sleep 2

      # launch dnsmasq dhcp  daemon
      #echo "INTERFACES=$BROADCAST" > /etc/default/dhcp
 
      dnsmasq  --conf-file=/etc/dnsmasq.conf
      sleep 1
      echo "DHCP Server Started..."

elif [[ $1 == "-1" || $1 == "--stop" ]]
   then
      # send signal 2 to hostapd and dnsmasq
      echo "Stopping DHCP Server..."
      sleep 1
      /etc/init.d/dnsmasq stop
      echo "DHCP server stopped..."
      sleep 1
      echo "Killing hostapd, dnsmasq"
      killall -2 hostapd dnsmasq
      sleep 1
      echo "All process killed.."
elif [[ $1 == "-2" || $1 == "--ics" ]]
   then
      echo "Starting Internet connection sharing..."
      # create iptables rules
      iptables -A FORWARD -i $RECEIVE -o $BROADCAST -s 192.168.0.1/24 -m conntrack --ctstate NEW -j ACCEPT
      iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
      iptables -A POSTROUTING -t nat -j MASQUERADE
      # set kernel variable(s)
      echo 1 > /proc/sys/net/ipv4/conf/all/forwarding
      # edit kernel configuration
      cp /etc/sysctl.conf /etc/sysctl.conf.ap_ctl
      echo "net.ipv4.conf.default.forwarding=1" >> /etc/sysctl.conf
      echo "net.ipv4.conf.all.forwarding=1" >> /etc/sysctl.conf
      sleep 1
      echo "Internet connection is now shared over wifi..."
      echo ""

     # To stop internet sharing
  
 elif [[ $1 == "-3" || $1 == "--noics" ]]
   then
      echo "Stopping Internet connection sharing..."
      # remove iptables rules
      iptables -D FORWARD 1
      iptables -D FORWARD 1

      # set kernel variable(s)
      echo 0 > /proc/sys/net/ipv4/conf/all/forwarding

      # revert kernel configuration
      mv -i /etc/sysctl.conf.ap_ctl /etc/sysctl.conf

      # restart networking
      #/etc/init.d/networking restart
      sleep 1
      echo "Internet connection sharing stopped..."
 else
 echo $0
 echo "A tool to manage hostapd and dnsmasq DHCP server"
 echo "Usage:"
 echo "    -0 --start    Start hostapd and dhcpd3"
 echo "    -1 --stop    Stop hostapd and dhcpd3 with signal 2"
 echo "    -2 --ics    Activate internet connection sharing"
 echo "            between specified interfaces"
 echo "    -3 --noics    Undo internet connection sharing settings"
 fi

exit 0 

Save it.  Exit the text editor. Now run

$ sudo chmod a+x /usr/bin/wifi_ap

Now you can run this script as command. This script has 4 options i.e  start , stop , ics and noics .

To start the hostspot , you should run

$ sudo wifi_ap --start

To stop the hostspot ,  you can run

$ sudo wifi_ap --stop

To share internet  connection over the hostspot

$ sudo wifi_ap --ics

To stop sharing internet connection ,

$ sudo wifi_ap --noics.  

Hope you enjoy it.



No comments: