Friday, April 27, 2007

Nokia N800 / Linux Laptop Ad-Hoc Connection Howto

OK, so I just got a Nokia N800 Internet tablet. I figured, I could use it as a laptop replacement in some cases when I don't feel like lugging around the big machine. To make a long story short (this is a tech post after all... so let's not get carried away...;-), the device , in its default config , does not help much without an internet connection. The N800's primary method of "hooking up" is Wifi. So I ran into this situation where Wifi internet connections were unavailable but I had my Linux laptop that used a wired connection to the internet. The laptop also has its Wifi option, which in this case was unused... Doh!

For reasons I don't want to disclose (;-) I really wanted to get on the Net with my N800. I could have used my mobile phone as a modem and pay Cingular generously for that. But I didn't find so attractive. What I really wanted to do, was to connect the N800 to my laptop through wifi ad-hoc mode and use the laptop as a gateway for the N800 to get on the Net via the laptops wired connection. Got that? (Note: the same scenario applies on many other situations including making your own access point at home using a desktop PC that has Wifi capabilities).

So here is how...

1. Configure laptop Wifi for ad-hoc mode

I have tried this with two laptops, an Lenovo/IBM T60 and an HP DV2000 both running openSuse Linux 10.2. The instructions below should be the same for pretty much any distribution but there is a possibility that some hardware configurations (read: Wifi chip-sets) might have trouble doing this under Linux...

Also... on the DV2000 I used the Windows drivers for the Broadcom Wifi chip-set through ndiswrapper.

So where was I...? Ad-hoc mode... Right. So, assuming wlan0 is the wifi interface, get a terminal and do:

linux> su -
linux# iwconfig wlan0 essid ADHOCNET mode ad-hoc
linux# iwconfig wlan0 key "s:0123456789abc"

Substitute "ADHOCNET" for your desired ESSID. I am also setting this up for 128-bit WEP encryption... we don't want to leave our machine vulnerable right? So substitute "s:0123456789abc" for your ASCII encryption key of choice. (Note: the key string as used here must be exactly 13 characters. If that seems weird, check out the iwconfig man page).

2. Configure Routing, NAT and MAC Address Filtering

The deal here is the following: the laptop needs to be told what to do with packets coming from the N800 and how to get packets back to it. At the same time, we don't want anyone just connecting to this ad-hoc network and gaining internet access that way... if it's not secured, this little ad-hoc network could be a loop hole to a corporate or home LAN. Generally we don't want that...

The commands below assume that there is currently no firewall of any kind running on the system. Additionally, the "iptables" tool and corresponding kernel modules need to be available. Most distributions will install these but in case there are any errors when the commands below are executed, make sure these requirements are fulfilled. So Again, in a terminal window on the laptop:

linux> su -
linux# echo 1 > /proc/sys/net/ipv4/ip_forward
linux# iptables -t filter -A INPUT --match state --state RELATED,ESTABLISHED -j ACCEPT
linux# iptables -t filter -P INPUT DROP
linux# iptables -t nat -A POSTROUTING --out-interface eth0 -j MASQUERADE
linux# iptables -N macfilter
linux# iptables -A macfilter -m mac --mac-source 00:19:4F:AB:CD:EF -j RETURN
linux# iptables -A macfilter -j REJECT
linux# iptables -A FORWARD -m state --state NEW -j macfilter
linux# ifconfig wlan0 10.10.0.1 netmask 255.255.255.0 up
I don't want to go over every line here. Freely consult the iptables man page as necessary ;-). Couple of things though: "--out-interface" must be set to what ever interface corresponds to the wired interface. The "--mac-source" parameter should be set to the MAC address of the N800.

Note: this is a minimal security configuration, designed to protect the wired network. Depending on environment, it would be recommended to generate a more complete firewall and add the NAT and MAC filter to that.

3. Configure the N800

Fire up the N800 and open "Tools > Control Panel". Select "Connectivity > Connections". Tap "New" to start the connection wizard. Give the connection a name... be creative... and select WLAN.

You'll be asked whether you would like to scan for a network. You can do that and if you do, select
"ADHOCNET" or whatever ESSID you used above in step 1. After selecting the network you want to connect to make sure that "Network mode" is "ad-hoc" and that "Security method" is "WEP". When asked, enter the WEP key you specified above in step 1.

When the "Connection setup complete" screen is displayed, tap "Advanced > IP Addresses". Un-ckeck "Auto-retrieve IP address" and enter:
IP address: 10.10.0.2
Subnet mask: 255.255.255.0
Router: 10.10.0.1

Scroll down and set at least the "Primary DNS address". I used the DNS from the laptop and you can get that by typing "cat /etc/resolv.conf" from a terminal.

Select "OK", "Finish", "Done" and "OK" and you should be OK, finished, done and OK.

Hope this might actually help someone. Otherwise I typed this all for nothing... ah well...