Android Mobile as Wifi Repeater

Technically, wifi repeater means your android mobile wifi network card will be connected to some wireless network as station (STA), meanwhile, the same wifi network card starts a access point (AP) and provide connectivity to other devices. If you are interested, keep on reading.

Why in hell would I do that? Well, I live in China, which is a amazing country, we all love. There is a so called “Great Firewall of China”. To by-pass that wall, I have to carry a small wifi router (tplink-wr703n) everyday, everywhere. 703n provides me a free wifi network, after connected to it, I can tweet at twitter and watch video at youtube. But that was not ideal, because I already have a such crowded bag. Also, I want to keep online while I am on the road, there can be no better device to do that than a mobile phone.

The biggest flaw of this dream is, well, mobile phone is not a router. In the very beginning, mobile lives at the edges of the network, it can either receive 3g signal, or receives wifi signal. After some years, mobile start to have the ability to work as access point (AP), which is called Personal Hotspot on iPhone, and SoftAP on Android. Although, mobile can be AP now, it is still not quite a router yet. If mobile is connected to wifi network before you start the AP, the connection will be broken up after you started the AP. The workable way is to make the phone work as a router is connect the internet via 3G, and provide access point via wifi. But that is just lame, why I should pay 3G bill while I can use wifi? Isn’t there any way to make my mobile connect to the internet via wifi while still serve as AP to other devices? That is a dream of me for a very long time. Now, the dream come true. Let’s see how that is possible.

First of all, what do I mean by wifi repeater. There are couple of ways to “repeat” a wifi network. The most comprehensive write-up on this area is this article: Client Mode Wireless. The wifi-repeater I am referring to is the “Routed Client Mode - Masqueraded” described in the article. Essentially, what we need is to have two wifi interfaces bounded to two ip addresses, and then use iptables to route one into another. The difficult part is obviously how to start two wifi interfaces when physically we only have one.

In general, there are two ways to get two wifi interfaces on Android mobile phones. If the wifi “chipset/firmware/driver/os” supports mac80211 (this equally translated to texa instrument wl12xx series), then we can just use “iw” command to add new interface. For other wifi chipsets, we have to use something called wifi direct to get two wifi interfaces. Wifi chipset used by mobile phone is different from wifi chipset used in laptop or infrastructure devices. It is designed to use minimal power instead of maximum capbility. The wifi chipset is like a small machine talk to the mobile cpu via SDIO interface. So, the support of certain feature is not just about software, even not just about driver. It requires the small machine attached via SDIO interface to support it as well. The barrier we need to overcome to get a phone supported is:

1. Hardware

2. Hardware Firmware (the OS of the small machine attached via SDIO)

3. Android Wifi Driver

4. Android Wifi Command

5. Script (issues the right wifi command in right sequence)

=== User Space === Script V V Wifi Commnad(iw, p2p-cli, etc) V [Via Netlink Socket] === Kernel Space === V Android Wifi Driver (such as bcmdhd) V [Via SDIO] === Small Machine attached via SDIO == V Wifi Chipset Firmware (such as fw_bcm4330.bin) V V Wifi Chipset (such as bcm4330）

Point 1, 2, 3 is the hard part. Either mac80211 or wifi direct requires we get Point 1, 2, 3 right. There is very few things we can do if after some experiment and find out your phone is not suported, due to the wifi chipset. The only thing we can do is to buy the correct hardware. Luckily, thanks Google, it introduced the API support of Wifi Direct in the Android 4.0 release. Nowdays, there are a lot of Android mobile phones that have the right chipset/firmware/driver to make wifi repeater working.

Before I buy a mobile phone, how can I tell if it supports wifi repeater or not? The easist way is to find out what kind of wifi chipset it is using. There are 4 companies that are strong in the market, producing wifi chipsets to nearly all mobile phones (including iPhone):

* Broadcom: bcm4325, bcm4329, [bcm4330, bcm4334, bcm43241]

* Texa Instrument: [wl12xx]

* MediaTek: [mt6620, mt6628]

* Qualcomm: wcn1312, wcn1314, ar6002, ar6003, [wcn3660]

Chipsets in the bracket are supported. Besides chipset, we also need to make sure the Android ROM we flashed have right wifi chipset firmware (p2p enabled) and kernel is compiled with nl80211 support.

If we can get point 1, 2, 3 right. Point 4 is easy, we just need to cross compile the wifi command to arm-eabi, copying to the mobile, and then it should work. Point 5 works like this:

1. Use mac80211 or wifi-direct to make a secondary wifi interface

2. Start AP on that interface

3. Assign ip address to newly created interface

4. Start DHCP server listening on the interface, so clients connected in can have ip addresses assigned

5. Setup iptables rules, masquerade from one network to another.

The exact script I am using is at: https://github.com/fqrouter/fqrouter/blob/master/manager/wifi.py

If you are too busy to read the lengthy code, here is the magic command:

iw wlan0 interface add ap0 type managed

wpa_cli p2p_group_add persisent=3

The limitation of wifi repeater is the upstream/downstream wifi must operate at same channel. This limitation is actually a feature called “Single Channel Concurrency”. As Google employee has pointed out in the thread https://groups.google.com/forum/?fromgroups=#!topic/android-platform/tLLspmSySbY, there are going to have more and more chipset support “Multi Channel Concurrency”. So wifi repeating is not some accidental feature, Android mobile happened to support. It is actually part of wifi direct spec. The feature will not go away, instead it is expected to be better supported in future hardwares.

I am making a software that allow me by-pass GFW, while share wifi to other devices so that they can enjoy all the benefits using the method describe above, called http://fqrouter.com