Saturday, April 28, 2012

PirateBox under Ubuntu

So I took on a project once again that I have already started several times before. Hoping that now I could solve all the issues and make it work. 

I am not the inventor of PirateBox, I was using script packages created by others and also received help with my work from several people on forums. I was following the DIY (Do It Yourself) tutorials for installing PB directly on a laptop find here.


Running a PirateBox from Ubuntu 11.10
If you don't know about PirateBox (PB), shortly summarised its an anonymous file sharing LAN network with integrated chat function. Perfect for sharing files over wifi and chatting with people around you either at parties,  at home or basically anywhere you want.

Read more about the PirateBox project here

***

How PB works

PirateBox is not a single program running, not a one click install from the software centre. It is a range of scripts and daemons running together that finally turn your  wifi card into an open wifi network. Here people can connect and get automatically redirected to the main PB share page with the chat function and up/download. 

So the following things need to be done by the "software package":
  • First of all it has to turn your wifi card into an access point, creating a wifi network, SSID
  • It has to handle the connected users, managing local IP addresses
  • It has to manage up- and downloads
  • Needs tu run a simple chat client where people can talk
Difficulties
  • Not all wifi cards support AP mode (access point)
  • Differences in every single Linux system make it hard to create a "universal" installer or tutorial
  • Driver issues, supported devices
What you need
  • python
  • hostapd - with nl80211 header support
  • dnsmasq

Python comes installed by default, so there was nothing to do here. If you removed it manually or haven't got installed for any reason simply
sudo apt-get install python

The same way, dnsmasq can be installed without any trouble
sudo apt-get install dnsmasq

Hostapd on the other hand has causes quite some troubles. (Hostapd stands for host access point daemon) The main point about PB is that is has to turn you wifi card into an AP as mentioned before. No hostapd is doing this with the nl80211 header/deriver. The problem was that this driver isn't included in hostapd for Ubuntu 11.10.

So when I went to install hostapd with simply 
sudo apt-get install hostapd
and then tried to run it based on a simple config file
sudo hostapd -dd hostapd.conf
I received an error, saying
error: nl80211 generic netlink not found.
error: nl80211 driver not found.

Now I have tried to compile the package and allow the nl80211 header in the package many times, but it just wouldn't work. the tutorial I followed for this was on linuxwireless.
No matter how many times I tried to compile it and run the test above, the same error was returned over and over again.

With some help from forums and quite some time spent searching for possible errors, it turned out that the problem was with my wireless driver and firmware and not really with hostapd.

In the end I could install hostapd with
sudo apt-get install dhcp3-server hostapd
And it was running fine.

Wireless card issues

I have a Broadcom BCM 4312 wifi card in my laptop that in Ubuntu 11.10 ships by default with the "Broadcom STA proprietary wireless driver". This is not good, so remove it. Easiest way to remove it is using the synaptic package manager, that once again doesn't come installed in Ubuntu 11.10 so install that first by
sudo apt-get install synaptic

Now that synaptic is installed run it and search  your STA driver and mark it for removal. Also search for b43, add 2 things:
b43-fwcutter 
firmware-b43-lpphy-installer
and install those. (I had to use lpphy version of the firmware, depends on your card)
Find more detailed description in this thread, ubuntuforums.

Run
sudo modprobe -v b43
to see if it works.



Installing PB

Now that background requirements are are all installed and driver issues solved, the actual PirateBox can be installed. There is a detailed tutorial for this HERE so I will not go into details. One thing to mention is that you can either download the package and install manually or use Piratebox-Manager.sh script package



Running PB

There are a few things in Ubuntu that you have to do to run PB correctly. However these are also mentioned in the installation tutorial I put them down here again the way they worked for me.

sudo service network-manager stop
sudo killall dhclient
sudo killall dnsmasq
sudo iptables -t nat -A PREROUTING -i wlan0 --protocol tcp --match tcp --destination-port 80 -j DNAT --to-destination 192.168.77.1:80
sudo iwconfig wlan0 mode managed
sudo /etc/init.d/piratebox start

This is the way I start my piratebox and it works.
  • Network manager has to be stopped before starting the script or it will reset your wifi card and disable AP mode
  • dhclient and dnsmas have to be stopped in case they are running so they can be started with the proper configuration
  • Sometimes the Ubuntu firewall (ufw) is causing problems, so run the command and redirection will run smooth
  • Also in case your wifi card is not in managed mode it must be set

After all these steps are done, piratebox can be started with
sudo /etc/init.d/piratebox start

The piratebox project is still under development and I am also  looking into running it more efficiently, checking what's needed or not. But for now, the start, its up and running.

3 comments:

  1. Does this take into account an ethernet card and keeping the routing normal on that, while still allowing the Wireless to be an access point for Piratebox?

    ReplyDelete
  2. Hello, I am not sure as I have not used PirateBox under my Ubuntu system for a while ( I am using the MR3020 router now), but I would say no, you cannot connect to the internet with your ethernet card.

    As far as I remember, when starting the PirateBox service it redirects all traffic to the website piratebox.lan. When testing, even though I wasn't connected to the wireless network (because my card was making it and cannot connect to itself) I was still redirected to the site in my browser. This lets me assume that this is a system wide redirect.
    Another reason for which I would say that you can't get an internet access at the same time is that the network manager has to be stopped that handles connections and it just would recognize you cabled connection.

    As I said its been a while since I used PB on my laptop, so I might be wrong about this. If you happen to test this, please tell me if it worked.

    ReplyDelete