0

I want to create a WiFi Access Point (AP) with hostapd and dnsmasq on my USB wireless network adapter--9271 Atheros using the Internet connected to my router through my eth0 adapter.

lsusb  
Bus 003 Device 003: ID 0cf3:9271 Atheros Communications, Inc. AR9271 802.11n

The MAC for the 9271 Atheros is 00:1a:8c:30:08:b1.
The MAC for my Android phone is f0:79:59:cb:d8:eb

The data of my wireless NIC is:

Output to: iw list:

Supported interface modes:
* IBSS
* managed
* AP
* AP/VLAN
* monitor
* mesh point
* P2P-client
* P2P-GO

Contents of: /etc/network/interfaces:

auto dsl-provider
iface dsl-provider inet ppp
pre-up /bin/ip link set eth0 up
provider dsl-provider

auto eth0
iface eth0 inet static
address 192.168.22.1
netmask 255.255.255.0
dns-server 114.114.114.114
auto wlan0
iface wlan0 inet dhcp

Contents of: /etc/hostapd/hostapd.conf:

hw_mode=g
channel=11
driver=nl80211
ssid=test
interface=wlan0    
# encryption
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
rsn_pairwise=CCMP
ctrl_interface=/var/run/hostapd    
# Only root can configure hostapd
ctrl_interface_group=0

Contents of /etc/dnsmasq.conf:

log-queries
log-facility=/var/log/dnsmasq/dnsmasq.log
interface=wlan0
listen-address=192.168.22.1
dhcp-host=f0:79:59:cb:d8:eb,192.168.22.3,infinite
dhcp-option=3,192.168.22.1
dhcp-option=6,114.114.114.114

To start the AP, I perform:

nmcli radio wifi off    
rfkill unblock wlan
ifconfig wlan0 192.168.22.2 up
sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -P FORWARD ACCEPT
sudo iptables --table nat -A POSTROUTING -o wlan0 -j MASQUERADE
service dnsmasq restart
service hostapd restart
hostapd /etc/hostapd/hostapd.conf

to connect to the AP with my Android phone with SSID test and password password.

The info displayed on the PC's console is:

hostapd /etc/hostapd/hostapd.conf
Configuration file: /etc/hostapd/hostapd.conf
Using interface wlan0 with hwaddr 00:1a:8c:30:08:b1 and ssid "test"
wlan0: interface state UNINITIALIZED->ENABLED
wlan0: AP-ENABLED 
wlan0: STA f0:79:59:cb:d8:eb IEEE 802.11: authenticated
wlan0: STA f0:79:59:cb:d8:eb IEEE 802.11: associated (aid 1)
wlan0: AP-STA-CONNECTED f0:79:59:cb:d8:eb
wlan0: STA f0:79:59:cb:d8:eb RADIUS: starting accounting session 57921245-00000000
wlan0: STA f0:79:59:cb:d8:eb WPA: pairwise key handshake completed (RSN)

When I try to connect to the AP with account name test and password password from my Android phone, it does connect, but no web page can be opened on my phone's Firefox,

I suspect an error in this section:

sudo sysctl -w net.ipv4.ip_forward=1
sudo iptables -P FORWARD ACCEPT
sudo iptables --table nat -A POSTROUTING -o wlan0 -j MASQUERADE

How should I proceed?

@Fabby,if hw_mode=n,an error occur as following:

hostapd /etc/hostapd/hostapd.conf
Configuration file: /etc/hostapd/hostapd.conf
Line 2: unknown hw_mode 'n'
1 errors found in configuration file '/etc/hostapd/hostapd.conf'
Failed to set up interface with /etc/hostapd/hostapd.conf
Failed to initialize interface

Please observe the output info of command hostapd /etc/hostapd/hostapd.conf.

Configuration file: /etc/hostapd/hostapd.conf
Using interface wlan0 with hwaddr 00:1a:8c:30:08:b1 and ssid "test"
wlan0: interface state UNINITIALIZED->ENABLED
wlan0: AP-ENABLED 
wlan0: STA f0:79:59:cb:d8:eb IEEE 802.11: authenticated
wlan0: STA f0:79:59:cb:d8:eb IEEE 802.11: associated (aid 1)
wlan0: AP-STA-CONNECTED f0:79:59:cb:d8:eb

The mac address of wlan0 is 00:1a:8c:30:08:b1 which is USB wireless network adapter at first ,then mac address of wlan0 changed into f0:79:59:cb:d8:eb which is my android phone's mac address,is that action right?

  • Connect your phone over USB and run adb logcat to see what is happening on the Android side. – Aeyoun Jul 21 '16 at 13:39
  • Can you try with iptables` disabled and if that doesn't work, setting the hw_mode to n. Also: do you have another Ubuntu Machine that you can connect to the AP as now you need an Ubuntu AND an Android expert. – Fabby Jul 23 '16 at 11:33
0

I see that you are allowing the traffic in the forward table of your iptables configuration however this is only in fact used when IP forwarding is in fact enabled for the interfaces in question.

By default ubuntu and in fact most linux distributions that I have seen disable IP forwarding for security which is in fact a sane default since this is only actually needed if your box is a router, in this case that is exactly what you are setting up so you must enable IP forwarding.

In the file /etc/sysctl.conf you need to uncomment this line:

net.ipv4.ip_forward=1

If you also wanted to forward IPv6 traffic you would need to uncomment this line too and set up the proper IPv6 configuration with the prefix assigned by your ISP.

net.ipv6.conf.all.forwarding=1

These will ensure the correct setting are set on boot if you want to avoid rebooting you can use commands like the following to enable forwarding immediately for the current session

For IPv4:

sudo sysctl -w net.ipv4.conf.[interface].forwarding=1

For IPv6:

sudo sysctl -w net.ipv6.conf.[interface].forwarding=1

Where [interface] is the name of the interface on which to enable forwarding this will need to be enabled for both the interface which connects to the internet so traffic can be forwarded back to the wireless network and for the interface connected to the wireless network.

Additionally your MASQUERADE rule looks incorrect to me it should MASQUERADE for traffic going out of the interface which connects to the internet and not traffic outbound to the wireless clients which is what you have.

Thus if your internet router is connected to the PC by Ethernet your rule will probably look something like this:

sudo iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE

Where eth0 is the interface which is connected towards the internet.

As for the error from hostapd hw_mode=n is invalid the configuration should be as follows:

hw_mode=g
ieee80211n=1

Thus 802.11n will be automatically negotiated if both devices support it, you can force the requirement for 802.11n at the cost of preventing some devices that don't support it from connecting at all with the following:

require_ht=1

I hope that helps if not I can try to help more but this is how I have mine configured and it works happily with all my android and fireOS devices.

Edit: Sorry just saw that you tried changing to hw_mode=n in response to the comment you had it right in the first place though I'd recommend allowing 802.11n for compatible devices with the ieee80211n=1 option since the card you have supports it and it will enable n capable devices to operate at higher speeds while still supporting less capable wireless clients as long as you don't force it (The require_ht=1 flag would disconnect non 802.11n capable clients). There isn't any reason to force it however as 802.11n and 802.11g can be supported simultaneously.

  • hostapd /etc/hostapd/hostapd.conf – it_is_a_literature Aug 19 '16 at 13:47
  • Configuration file: /etc/hostapd/hostapd.conf HT (IEEE 802.11n) with WPA/WPA2 requires CCMP/GCMP to be enabled, disabling HT capabilities – it_is_a_literature Aug 19 '16 at 13:47
  • nl80211: Could not configure driver mode nl80211 driver initialization failed. hostapd_free_hapd_data: Interface wlan0 wasn't started – it_is_a_literature Aug 19 '16 at 13:47
  • hostapd /etc/hostapd/hostapd.conf when to set require_ht=1 – it_is_a_literature Aug 19 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.