Bluetooth Network Aggregation Point

This article will deal with getting Gentoo to act as a Bluetooth Network Aggregation Point "NAP".

What is a NAP
The Bluetooth specification incorporates the ability to create a Personal Area Network "PAN". It is an Ethernet transparent protocol, thus all standard protocols (especially IP) can be used in such a PAN. A NAP can be thought of being the Master in such a network. It will provide the connectivity to other networks (Internet for instance) for up to eight via Bluetooth connected PAN devices (cell phones, PDAs, laptops maybe).

Scope of this article
We will deal with the creation of a Gentoo "NAP" in order to connect a Bluetooth enabled cell phone with the Internet. Then, for instance one could synchronize the cell phone's contacts/calendar with a syncml capable groupware server located anywhere on the Internet. We will henceforth call the NAP-Server "Gentoo-Box" and the PAN-Client "cell phone".

Software
You need to at least emerge net-misc/bridge-utils net-wireless/bluez-4.101-r1 or higher for this. This howto will deal with the new Bluez-4 way. You will need "test-programs" enabled as a USE flag.

Pairing
One part that took me quite some time to figure out was the pairing part. If you already paired your Gentoo-Box with you cell phone you can skip right down to the next paragraph.

Make sure your Bluetooth dongle is plugged into your Gentoo-Box. On some laptops you might still need to activate the build in Bluetooth dongle. Once it is connected call

hci0:  Type: USB BD Address: AA:AA:AA:AA:AA:AA ACL MTU: 377:10 SCO MTU: 64:8 UP RUNNING PSCAN ISCAN RX bytes:1409 acl:0 sco:0 events:51 errors:0 TX bytes:469 acl:0 sco:0 commands:52 errors:0 as root. Wirte down your Bluetooth hardware address which in this case is AA:AA:AA:AA:AA:AA. There should be a folder with your BD Address as a name in

total 1 drwxr-xr-x 3 root root   88 Jul 26 09:18. drwxr-xr-x 49 root root 1376 Jul 25 19:48 .. drwxr-xr-x 2 root root  296 Jul 27 09:42 AA:AA:AA:AA:AA:AA which contains a file called "config". In order to have your BD-dongle in discoverable mode, add the last line of this example to it

Now, have your cell phone search for BT devices in range. It should display your Gentoo-Box in the Bluez way, as "Gentoo-Box-0". You can try to connect from your cell phone and get prompted for a PIN on the cell phone. Enter any pin you like. You either get a pinentry-dialog on your Gnome/KDE desktop to enter the same pin as before(if successful, your are done with this paragraph), or your cell phone might respond, that it was not able to pair (continue reading).

We need your cell phone's BD Address, so if you don't have it jet, set your phone to discoverable mode (remember to disable this mode later on, as people suggest it might be a security problem, if on the road). Execute

Scanning ... BB:BB:BB:BB:BB:BB      CellPhone and you will have your cell phone's BD address displayed. If not already there, create the file

Every line stands for a single Bluetooth device, separated by "whitespace" from a pairing pin. In this example, the BB:...:BB line says, the cell phone is known, and should a pairing be asked for, use "9876" as a pairing pin. Restart the bluetooth daemon

and initiate a pairing from your cell phone. Use the pin provided in the config file "9876" for your cell phone, and this time, the pairing should be successful.

Edit: Actually, bluez-4 is bundled with "simple-agent" utility, so in order to do pairing, just issue in your terminal (maybe even as a usual user)

then try to connect to the computer from the phone. The latter will ask you to enter a PIN, and you should write anything you want. After you entered it, simple-agent on Gentoo box will, in turn, prompt for the PIN (enter the same value as before:)

Accepting connections
As Gentoo needs to be told, that the cell phone is allowed to connect to the Gentoo-Box (yes, even if they are paired alread) create/edit the file

so you allow the cell phone to connect/use any Bluetooth service provided by your Gentoo-Box.

Bluez 4.x
I had a problem with my Broadcom bluetooth dongle with bluez 4.62. If something goes wrong don't forget, that you can delete. The problem is, that aforementioned actions are not enough to perform pairing. The paired device ( BB:BB:BB:BB:BB:BB) should be known, trusted and have a pincode. To archive that install test-programs of bluez 4.x:

Than make your device known to bluez:

To make your device trusted to bluez do:

To make your adapted discoverable you can do:

I don't know how to create pincode with bluez-test utils, so you should create it manually.

There is an bluez-test-network utility, that you can use for testing PAN-connection, initiated from server side and monitor-bluetooth utility to watch what is going behind the D-bus scene, because watching /var/log/messages can be not enough.

PS. For unknown reason simple-agent script was not worked in Bluez 4.62 for me.

PPS. in Bluez 4.62 /etc/init.d/bluetooth script is totally udev based. So, there is no sense to do /etc/init.d/bluetooth restart. Probably, you should restart dbus/udev or even reboot the computer to feel changes in /etc/bluetooth/*

Since bluez-4.101-r1 /etc/init.d/bluetooth is NOT udev based anymore. Restart will stop and start "bluetoothd".

Setting up a network bridge
As the Bluez NAP-Server relies on Gentoo to handle the Ethernet link created by Bluez we will create an Ethernet bridge, so we will be able use the full power of the Gentoo startup scripts. Make sure to backup all files before editing them.

We will deal with the following scenario as I think this to be the easiest and most common case. Internet <-- Global IP Address --> Router (with DHCP and routing (NAT) capabilities) <-- local IP Address --> Gentoo-Box Execute

eth0     Link encap:Ethernet  HWaddr 00:e0:f4:19:c4:25 inet addr:192.168.0.2 Bcast:192.168.0.255  Mask:255.255.255.0 inet6 addr: fe80::2e0:f4ff:fe19:c425/64 Scope:Link inet6 addr: 2001::2/64 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500  Metric:1 RX packets:108069 errors:0 dropped:0 overruns:0 frame:0 TX packets:152560 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:50247357 (47.9 MiB) TX bytes:25484155 (24.3 MiB) Interrupt:27 Base address:0x6000 to locate the network card which connects to the router. The line inet addr:192.168.0.2 Bcast:192.168.0.255  Mask:255.255.255.0 is the one you should be looking for. Remember the device name (eth0) and find

change this to read

and add the following lines

which tells Gentoo to not ask for a dhcp-lease (actually it tells gentoo to do nothing for eth0) on eth0 and create a new bridge br1, which contains eth0. As eth0 used to get its IP Address via dhcp, we set br1 to do exactly that. The options "stp on" and "setfd 1" make sure, the Spanning Tree Protocol is used, and the forwarding state of the bridge will be reached within one second of a topology change. The last options speeds up the cell phone connection considerably, as gentoo tends to default to around 10 seconds, thus every cell phone connect would take at least that long.

Create a new symbolic link

and try (careful, you will lose your internet connectivity for this time, if things break, restore your backup of

and execute in order to regain internet access, and try again.) to execute

... ... Check, if you are connected to the web (ping en.gentoo-wiki.com). If everything works well, add br1 to your default runlevel
 * Starting br1
 * Creating bridge br1 ...                                                                            [ ok ]
 * Adding ports to br1
 * eth0 ...                                                                                         [ ok ]
 * Bringing up br1

Finally connecting your cell phone to the Internet
After these preparations, things are easy. Start the NAP service and notice interface br1. Enter in a terminal

Watch

Jul 27 12:53:19 gentoo bluetoothd[11077]: link_key_request (sba=AA:AA:AA:AA:AA:AA, dba=BB:BB:BB:BB:BB:BB) Jul 27 12:53:19 gentoo bluetoothd[11077]: link_key_request (sba=AA:AA:AA:AA:AA:AA, dba=BB:BB:BB:BB:BB:BB) Jul 27 12:53:19 gentoo device bnep0 entered promiscuous mode Jul 27 12:53:19 gentoo br1: port 3(bnep0) entering listening state Jul 27 12:53:19 gentoo bluetoothd[11077]: Added new connection: bnep0 Jul 27 12:53:19 gentoo bluetoothd[11077]: bridge br1: interface bnep0 added Jul 27 12:53:20 gentoo br1: port 3(bnep0) entering learning state Jul 27 12:53:21 gentoo br1: topology change detected, propagating Jul 27 12:53:21 gentoo br1: port 3(bnep0) entering forwarding state when you tell your cell phone to connect to the internet using the Gentoo-Box as NAP. If things go this far, and the phone does not show any sign of being connected (no webpages can be loaded) try wireshark to figure out what packets are being send and recieved. Maybe a simple

can do the trick. Make sure no firewall settings on the Gentoo-Box interfere.

Modifying the bluez-test-nap script
If you want to get rid of the sleep timer in the standard bluez-test-nap script, or you want to run the NAP service in the background, you could make these small changes:

Copy the script to an alternate location, and apply the patch.

If /usr/local/bin comes first in your $PATH, you can still type:

To run the script in the background, and stop it:

To run at startup, let /etc/init.d/local read next scripts. These files must be made executable.