Debian Linux HowTo: Bridging WLAN to Ethernet for Access Point (Infrastructure Mode) for Android Phones

wlan card

I am using the Wireless (WLAN) mainly to connect my mobile phone to the internet, for faster downloads and to test mobile apps. First, I only used Apple devices (such as iPad, iPod, iPhone, etc.), and those could connect without problems to a so-called Ad-Hoc network. However, many mobile devices, such as Android, unfortunately will NOT connect to an Ad-Hoc network, for reasons which are very well explained on this How-To Geek posting (it taught me about many things I didn’t know!).Since I’ve recently given up on Apple products and only use Android phones, I needed a definite answer to this problem. And I found it today.

As you may know, there are basically 2 modes to operate a wireless network: “Ad-Hoc” and “Infrastructure” (the latter one also called “Access Point” or shorter “AP”) mode. Cheaper Wireless cards only support the Ad-Hoc mode, but do NOT support the AP mode.

So, I thought: Let’s just buy a WLAN card which supports the AP mode. I bought the Intel Centrino Advanced-N 6235 (Link to official Intel product page). I found it here on Amazon: Intel Network 6235AN.HMWWB Centrino WiFi Card Half Mini PCI Express Advanced-N 6235 Dual Band Bluetooth

It is supported beginning with Kernel version 3.2. I mounted it, and was planning to use the Network Manager of Debian Wheezy / Gnome 3 to quickly set up an AP Hotspot. Easier said than done! Because even though there is an option to set “Infrastructure” mode, my Android phone still would not connect. It would simply write “Ad-Hoc Connecting” and stop there, even though I had set the Infrastructure mode.

 

Network Manager in Debian Wheezy / Gnome3

Network Manager in Debian Wheezy / Gnome3

 

Network Manager in Debian Wheezy / Gnome3 offering "Infrastructure" mode which is not recognized by an Android smartphone, even though the network card supports AP and is fully supported by the Kernel.

Network Manager in Debian Wheezy / Gnome3 offering “Infrastructure” mode which is not recognized by an Android smartphone, even though the network card supports AP and is fully supported by the Kernel.

 

I had to wade through a LOT of web postings, all suggesting not-really-working ‘solutions’ until I found a synthesis of all the collected information that worked for me.

It turns out that you need a dedicated user-space program for ‘driving’ the AP hardware contained in your network chip, and this is hostapd . It seems, you can’t do without.

I found this blog which offered a bash script to automate the job, but it only worked for Ubuntu, and not for Debian due to different configuration files. I also found this little Qt program that made the wireless connection work for my phone, but disconnected my laptop from all networks.(It could have been my fault due to misunderstandings though…)

This blog did a good job in explaining what  hostapd  is and gives basic installation and configuration instructions. You should read the post in any event. However, the suggested final solution did not work for me. Yes, the phone would see the generated wireless network, would connect, but immediately disconnect. I have not found the reason for the failure, but it probably lies hidden in the complexity of the set-up which painstakingly involves

  • a locally run DHCP and DNS server,
  • configuration of same,
  • and manual calls to  iptables  (masquerading, forwarding, etc.) to route between eth0 and wlan0 devices

The author of this blog wrote a second post to avoid the locally run DHCP server in favor of re-using the DHCP server of the network (e.g. the modem) by using  dnsmasq , which is yet another locally run server, only a bit simpler, but has to be configured also. I tried it to no avail. Same problem: phone would disconnect immediately. Frustrated, I moved on.

Then I found ths blog which brought a new idea: the concept of bridging between network interfaces instead of using iptables, which, and this is the good news, is supported ‘natively’ by  hostapd and the Linux networking system configured by  /etc/network/interfaces . However, this solution still did not work for me. After modifying  /etc/network/interfaces accordingly, I managed to mess my internet connection up. It turned out that setting up a bridge between Ethernet and Wireless is not that trivial! Even the highly official Debian Wiki BridgeNetworkConnections would not give working results for my case. So, I started crossreferencing between other solutions and finally came up with the following sequence of commands that work for me on my pretty standard Debian Wheezy laptop with one ethernet adapter (eth0) and the above mentioned Intel network adapter (wlan1). You only need to install 2 packages (bridge-utils and hostapd). With my method, there is no need for a locally installed DHCP or DNS server, and no need to modify the /etc/network/interfaces  file. There is no requirement to permanently make changes to your network configuration. In an emergency (if you are stuck somehow), a reboot will reset your network configuration to the defaults. Devices connected to your newly created wireless network will be served by the DHCP and DNS server specified in your router/modem. Your mobile phone will be a regular and equal member of your LAN.

You can turn the following listed commands easily into a Bash script with ‘start’ and ‘stop’ arguments. But I’ll leave that up to you. It’s easy enough once you copy/past the following commands into a terminal window. So, without further ado, as superuser…

… install bridge-utils. These are helper programs.

Next, remove the attached IP address from the Ethernet card eth0. Bridging will not work when an IP address is set.

Next, turn on IPv4 networking for your wireless card. This is necessary, otherwhise one of the next steps will give the error “can’t add wlan1 to bridge br0: Operation not supported”.

Next, create the actual bridge br0 with the helper program brctl:

Next, bridge between ethernet and wireless. The order of the last two arguments is not important:

Next, bring the newly created bridge up, as a virtual device:

At this point, you will have lost your connection to your LAN/WAN. You have to set an IP address, netmask, router, etc. for the bridge. We do this via DHCP:

Now you should be able to access the internet again. Test it! If it doesn’t work, just reboot your computer. We have not made permanent changes to your system (another advantage of this method!)

So far so good. Now we have to create our actual wireless network in AP mode and use our Android phone to test it. Install hostapd:

Create a configuration file somewhere on your drive. I chose the location  /etc/hostapd/my-wlan.conf . Make sure you have the right driver set for your card (see above mentioned blog for more info. n180211 should work in most cases):

(comments are thanks to above mentioned blog)

Now, simply start hostapd with this configuration file as the only argument:

You will see debug messages helping you to see what is going on behind the scenes. Now use your Android phone, enable WIFI, wait for the network “networkname” to appear, connect to it, enter the password “12345678”, open up a browser and see if it works. Make sure that Wireless is turned on in your Network Manager:

gnome network manager wlan

Otherwise you will get the following messages:

To turn off wireless (you really don’t want to be grilled by microwaves 24 hours a day!), you cannot just turn Wireless off in your Network Manager since it seems to ‘damage’ the bridge (I have yet to confirm this a second time), and you won’t be able to access your network/internet. You have to ‘deconstruct’ the bridge in the following way:

Only after that, you can turn of Wireless in your Network Manager. Again, if you are stuck, just reboot.

I hope this helps somebody, it worked for me, if not, write me a comment.

 

4 Responses to Debian Linux HowTo: Bridging WLAN to Ethernet for Access Point (Infrastructure Mode) for Android Phones

  1. mathieu May 20, 2015 at 12:28 pm #

    Hello!

    Thanks, that did really help. I had to add two lines to my script in order for it to work and for my computer to regain internet access. It goes like this:

    # disable networkmanager before operation since it may interfere
    nmcli n off
    #may be necessary
    #rfkill unblock wlan0

    ip addr flush dev eth0
    iw dev wlan0 set 4addr on
    brctl addbr br0
    brctl addif br0 eth0 wlan0

    #you have to bring eth0 up again otherwise no internet access on your computer
    ip link set dev eth0 up

    ip link set dev br0 up
    dhclient br0

    hostapd

    ###

    Cheers,

  2. Kevin October 7, 2015 at 3:52 am #

    Thanks for the detail information. I just share my Ubuntu Wifi setting as an “Access Point” (Not Ad hoc) for Android Wifi connection. (1) Go to Ubuntu Software Center and install “kde-nm-connection-editor”. (2) Open it and click “Add”. Select “Wireless (Shared)”. (3) In the Wireless tab, change Connection Name, SSID name, and select “Access Point” in Select Mode. (4) Setup the connection password in Wireless Security tab. (5) Double check IPv4 tab, Method: “Shared to other Computers” (6) Close the program. Turn on the Wifi Access Point by click the wireless connection, select “Connect to hidden Wifi Network”, choose the SSID that was just created. (7) Turn on Android Wifi and connect.

  3. L. November 13, 2015 at 5:47 pm #

    4addr mode has nothing to do with “IPv4 networking”. 4addr mode enables transmission of 4 MAC addresses in each data packet to enable clients on one side of the bridged network to reach clients on the other site.

  4. srini June 30, 2017 at 12:46 am #

    I am intel5300 wifi card on miniPCIe .After having done the above procedure i see my br0 eth0 and wlan1 .I cannot hostapd to start properly where I get errors like

    nl80211: Could not configure driver mode
    nl80211 driver initialization failed
    hostapd_free_hapd_data:Inteface wlan1 wasnt started

Leave a Reply

Powered by WordPress. Designed by Woo Themes