TonyHuff
Posts: 5
Joined: Mon Feb 26, 2018 9:18 pm

WiFi Client + AP

Mon Feb 26, 2018 10:41 pm

I am unable to configure a RPi3 as both a client and AP. My goal was to connect to the AP using an Android and pickup internet access via the Client mode if it was configured. I can provide internet access as the direction seems to be from the Android to the AP but as soon as I try to do something from the AP to the Android (such as WebSockets or SSE or even ping) then packet loss makes it unusable.

I can access the pi via eth0, wlan0 and connect my android via ap0 but I am seeing massive packet loss when pinging from the pi via the ap to the android. Pings from / to any other direction have zero packet loss. I know this topic has been discussed thoroughly on several threads but I have been unable to get this going reliably.
If I ifdown wlan0 the pings via the ap are 100%, as soon as I ifup wlan0 the packet loss is enormous.
It is as though the RPi3 cannot act as both a client and an AP.

Apologies for the long post, I was trying to include relevant configuration information.

android - IP: 192.168.0.14
wifi Network router: 192.168.1.254
wlan0 - IP: 192.168.1.149
ap0 - IP: 192.168.0.1

pi@raspberrypi:~ $ ping 192.168.0.14 -I ap0
PING 192.168.0.14 (192.168.0.14) from 192.168.0.1 ap0: 56(84) bytes of data.
64 bytes from 192.168.0.14: icmp_seq=18 ttl=64 time=11.8 ms
64 bytes from 192.168.0.14: icmp_seq=37 ttl=64 time=2.40 ms
........
64 bytes from 192.168.0.14: icmp_seq=49 ttl=64 time=9.81 ms
64 bytes from 192.168.0.14: icmp_seq=52 ttl=64 time=1.29 ms
--- 192.168.0.14 ping statistics ---
53 packets transmitted, 11 received, 79% packet loss, time 53814ms
rtt min/avg/max/mdev = 1.295/4.543/11.834/3.283 ms
pi@raspberrypi:~ $

Using a ping tool from my android to the pi @ 192.168.0.1 I am seeing times of 8.3ms - 38.7ms.

If I Ping a workstation on the same network as the wlan0 wifi ...
pi@raspberrypi:~ $ ping 192.168.1.103 -I wlan0
PING 192.168.1.103 (192.168.1.103) 56(84) bytes of data.
64 bytes from 192.168.1.103: icmp_seq=1 ttl=128 time=3.01 ms
64 bytes from 192.168.1.103: icmp_seq=2 ttl=128 time=5.69 ms
.......
64 bytes from 192.168.1.103: icmp_seq=6 ttl=128 time=2.91 ms
64 bytes from 192.168.1.103: icmp_seq=7 ttl=128 time=2.18 ms

--- 192.168.1.103 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6008ms
rtt min/avg/max/mdev = 2.186/4.188/6.597/1.697 ms
pi@raspberrypi:~ $

Ping times from a lan workstation to the pi on wlan0 ...
C:\Users\User>ping 192.168.1.149
Pinging 192.168.1.149 with 32 bytes of data:
Reply from 192.168.1.149: bytes=32 time=6ms TTL=64
Reply from 192.168.1.149: bytes=32 time=6ms TTL=64
Reply from 192.168.1.149: bytes=32 time=5ms TTL=64
Reply from 192.168.1.149: bytes=32 time=2ms TTL=64
Ping statistics for 192.168.1.149:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 6ms, Average = 4ms
C:\Users\User>

So, I can ping between the Client wifi both ways without issue or loss of packets.
I can ping from the android device to the ap (the tool I use does not report lost packets but there are no delays and the times look ok).
When I ping from the ap to the android connected via the ap I get massive lost packets.

after entering ifdown wlan0 then enter pi@raspberrypi:~ $ ping 192.168.0.14 -I ap0
packet loss goes to zero and the ping times are 87ms to 368ms.

configuration files....
****************************************************************************
pi@raspberrypi:~ $ cat /etc/network/interfaces
#
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto eth0
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet dhcp
pre-up ifup ap0
pre-up sleep 2
pre-up wpa_supplicant -c /etc/wpa_supplicant/client.conf -i wlan0 -B
post-down killall wpa_supplicant

allow-hotplug ap0
iface ap0 inet static
pre-up iw phy phy0 interface add ap0 type __ap
address 192.168.0.1
netmask 255.255.255.0
post-down iw dev ap0 del
wpa-ssid "RPi3"
wpa-psk "raspberry"
pi@raspberrypi:~ $

****************************************************************************
pi@raspberrypi:~ $ cat /etc/hostapd/hostapd.conf
interface=ap0
driver=nl80211
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
wpa=2
ignore_broadcast_ssid=0
wpa_key_mgmt=WPA-PSK
#wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=raspberry
ssid=RPi3
ieee80211n=1
wmm_enabled=0
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
pi@raspberrypi:~ $

****************************************************************************
pi@raspberrypi:~ $ cat /etc/dnsmasq.conf
#log-facility=/var/log/dnsmasq.log
#log-dhcp
expand-hosts
interface=ap0
local=/home/
domain=home
bind-interfaces
domain-needed
bogus-priv
listen-address=192.168.0.1
dhcp-authoritative
dhcp-option=3,192.168.0.1
dhcp-range=192.168.0.10,192.168.0.25,255.255.255.0,12h
dhcp-option=option:router,192.168.0.1
server=192.168.0.1
server=8.8.8.8
pi@raspberrypi:~ $

****************************************************************************
pi@raspberrypi:~ $ cat /etc/wpa_supplicant/client.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US

network={
ssid="Network-ssid"
psk="Network-password"
}
pi@raspberrypi:~ $

****************************************************************************
pi@raspberrypi:~ $ cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address

#driver issues require a restart of the wireless
sleep 1
ifdown wlan0
sleep 1
ifup wlan0

#start the node webserver
#node /var/www/server.js

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

exit 0
pi@raspberrypi:~ $

Routing info....
****************************************************************************
pi@raspberrypi:~ $ ip route show table all
default via 192.168.1.254 dev wlan0
192.168.0.0/24 dev ap0 proto kernel scope link src 192.168.0.1
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.149
broadcast 127.0.0.0 dev lo table local proto kernel scope link src 127.0.0.1
local 127.0.0.0/8 dev lo table local proto kernel scope host src 127.0.0.1
local 127.0.0.1 dev lo table local proto kernel scope host src 127.0.0.1
broadcast 127.255.255.255 dev lo table local proto kernel scope link src 127.0.0.1
broadcast 192.168.0.0 dev ap0 table local proto kernel scope link src 192.168.0.1
local 192.168.0.1 dev ap0 table local proto kernel scope host src 192.168.0.1
broadcast 192.168.0.255 dev ap0 table local proto kernel scope link src 192.168.0.1
broadcast 192.168.1.0 dev wlan0 table local proto kernel scope link src 192.168.1.149
local 192.168.1.149 dev wlan0 table local proto kernel scope host src 192.168.1.149
broadcast 192.168.1.255 dev wlan0 table local proto kernel scope link src 192.168.1.149
2600:1700:bfc2:6cb0::/64 dev wlan0 proto kernel metric 256 expires 1209115sec pref medium
fe80::/64 dev ap0 proto kernel metric 256 pref medium
fe80::/64 dev wlan0 proto kernel metric 256 pref medium
default via fe80::d604:cdff:fe52:8c10 dev wlan0 proto ra metric 1024 expires 1315sec hoplimit 64 pref medium
unreachable default dev lo proto kernel metric 4294967295 error -101 pref medium
local ::1 dev lo table local proto none metric 0 pref medium
local 2600:1700:bfc2:6cb0:ba27:ebff:feef:2cd2 dev lo table local proto none metric 0 pref medium
local fe80::ba27:ebff:feef:2cd2 dev lo table local proto none metric 0 pref medium
local fe80::ba27:ebff:feef:2cd2 dev lo table local proto none metric 0 pref medium
ff00::/8 dev ap0 table local metric 256 pref medium
ff00::/8 dev wlan0 table local metric 256 pref medium
unreachable default dev lo proto kernel metric 4294967295 error -101 pref medium
pi@raspberrypi:~ $


version info....
****************************************************************************
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l GNU/Linux
****************************************************************************
pi@raspberrypi:~ $ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
pi@raspberrypi:~ $

****************************************************************************
pi@raspberrypi:~ $ cat /etc/rpi-issue
Raspberry Pi reference 2017-11-29
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, b71bf3179310aadf9aaf2ffcdf094a88e488dce8, stage5
pi@raspberrypi:~ $
****************************************************************************


If you have any tips / hints or observe any obvious problems some direction would be appreciated.
I am not an expert but I do know enough to get myself in serous trouble and I think perhaps I did.

TonyHuff
Posts: 5
Joined: Mon Feb 26, 2018 9:18 pm

Re: WiFi Client + AP

Wed Feb 28, 2018 1:25 pm

After many tests it appears that pings from the ap0 interface to a laptop have normal results with the Android being the device that exhibits errors with massive packet loss. Other tests with other Android phones present the same loss of packets. It seems laptops or other devices negotiate connection parameters differently than Android phones. I have been unable to change any configuration parameters (hostapd, interfaces, dnsmasq) that have the desired effect (although admittedly, I am not an expert with these configuration settings).
It is interesting that pings from the wlan0 interface to the Android work as expected with zero packet loss to the Android, only pings originating from the ap0 interface incur packet loss. Also, if the wlan0 interface is brought down then pings from the ap0 interface to the Android are successful without the loss of packets. The only condition that exhibits an issue is when the wireless device is simultaneously configured (with the interfaces up) in Client mode and AP mode. Even then the behavior is only seen when pinging Android devices.
More tests are being done to try to isolate the issue to a specific cause. Unknown at this time if this is a network configuration issue, driver issue, Android anomaly or wireless device problem. The iw commands list this configuration as supported for the RPi3 (Client + AP).

SurferTim
Posts: 1819
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: WiFi Client + AP

Wed Feb 28, 2018 2:00 pm

I've tested this and it might work for your application.
Add this to /etc/network/interfaces

Code: Select all

auto wlan0
iface wlan0 inet dhcp
	wpa-ssid "MySSID"
	wpa-psk "MyPassphrase"
Reboot. This will cause the dhcpcd service to fail.

Insure your OS is up to date.

Code: Select all

sudo apt-get update
sudo apt-get upgrade
Install the modules.

Code: Select all

sudo apt-get install dnsmasq hostapd
Edit /etc/rc.local and add this above "exit 0". Note that is two underscores on __ap.

Code: Select all

service hostapd stop
service dnsmasq stop
iw dev wlan0 interface add uap0 type __ap
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
ifdown wlan0
ip link set uap0 up
ip addr add 192.168.4.1/24 broadcast 192.168.4.255 dev uap0
service hostapd start
ifup wlan0
service dnsmasq start
The /etc/dnsmasq.conf file

Code: Select all

interface=uap0
dhcp-range=192.168.4.20,192.168.4.100,255.255.255.0,12h
The /etc/hostapd/hostapd.conf file. If you change the wpa_passphrase, it must be 8 to 63 characters. No shorter or longer!

Code: Select all

interface=uap0
driver=nl80211
ssid=RPiNet
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=mypassphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
Edit /etc/default/hostapd, uncomment and change this:

Code: Select all

DAEMON_CONF="/etc/hostapd/hostapd.conf"
Edit /etc/sysctl.conf and uncomment

Code: Select all

net.ipv4.ip_forward=1
Reboot.

TonyHuff
Posts: 5
Joined: Mon Feb 26, 2018 9:18 pm

Re: WiFi Client + AP

Wed Feb 28, 2018 11:33 pm

Thank you for the specific instructions SurferTim, I have entered each edit with careful double checking. After rebooting, my android was handed address 192.168.4.64 and the ifconfig output is .....

Code: Select all

pi@raspberrypi:~ $ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 200  bytes 16418 (16.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 200  bytes 16418 (16.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

uap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.4.1  netmask 255.255.255.0  broadcast 192.168.4.255
        inet6 fe80::ba27:ebff:feef:2cd2  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:ef:2c:d2  txqueuelen 1000  (Ethernet)
        RX packets 250  bytes 33267 (32.4 KiB)
        RX errors 0  dropped 5  overruns 0  frame 0
        TX packets 428  bytes 75449 (73.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.149  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::ba27:ebff:feef:2cd2  prefixlen 64  scopeid 0x20<link>
        inet6 2600:1700:bfc2:6cb0:ba27:ebff:feef:2cd2  prefixlen 64  scopeid 0x0<global>
        ether b8:27:eb:ef:2c:d2  txqueuelen 1000  (Ethernet)
        RX packets 5208  bytes 387293 (378.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9141  bytes 8521250 (8.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Pinging android ...

Code: Select all

pi@raspberrypi:~ $ ping 192.168.4.64
PING 192.168.4.64 (192.168.4.64) 56(84) bytes of data.
64 bytes from 192.168.4.64: icmp_seq=1 ttl=64 time=7.39 ms
64 bytes from 192.168.4.64: icmp_seq=5 ttl=64 time=111 ms
64 bytes from 192.168.4.64: icmp_seq=7 ttl=64 time=147 ms
64 bytes from 192.168.4.64: icmp_seq=14 ttl=64 time=17.4 ms

--- 192.168.4.64 ping statistics ---
15 packets transmitted, 4 received, 73% packet loss, time 14453ms
rtt min/avg/max/mdev = 7.399/70.939/147.057/59.932 ms
^Cpi@raspberrypi:~ $ 
This is the same result I was seeing previously.

If I disconnect from the AP and connect the Android to the WiFi network on wlan0 the Android is handed ip 192.168.1.137 and the ping results are...

Code: Select all

pi@raspberrypi:~ $ ping 192.168.1.137
PING 192.168.1.137 (192.168.1.137) 56(84) bytes of data.
64 bytes from 192.168.1.137: icmp_seq=1 ttl=64 time=33.6 ms
64 bytes from 192.168.1.137: icmp_seq=2 ttl=64 time=58.1 ms
64 bytes from 192.168.1.137: icmp_seq=3 ttl=64 time=83.8 ms
64 bytes from 192.168.1.137: icmp_seq=4 ttl=64 time=106 ms
64 bytes from 192.168.1.137: icmp_seq=5 ttl=64 time=21.5 ms
64 bytes from 192.168.1.137: icmp_seq=6 ttl=64 time=60.1 ms
64 bytes from 192.168.1.137: icmp_seq=7 ttl=64 time=80.8 ms
64 bytes from 192.168.1.137: icmp_seq=8 ttl=64 time=92.6 ms
64 bytes from 192.168.1.137: icmp_seq=9 ttl=64 time=135 ms
64 bytes from 192.168.1.137: icmp_seq=10 ttl=64 time=40.3 ms
^C
--- 192.168.1.137 ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9011ms
rtt min/avg/max/mdev = 21.521/71.313/135.008/33.447 ms
pi@raspberrypi:~ $ 
So the wlan0 traffic to the Android is just fine but the uap0 traffic to the Android causes a problem with lost packets.

SurferTim
Posts: 1819
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: WiFi Client + AP

Thu Mar 01, 2018 2:29 am

I have never claimed the sharing of one wireless device as both an ap and a client was efficient. Quite the opposite. I have always recommended using a separate wireless device for each because of that inefficiency.

TonyHuff
Posts: 5
Joined: Mon Feb 26, 2018 9:18 pm

Re: WiFi Client + AP

Thu Mar 01, 2018 12:50 pm

Thank you for your assistance SurferTim. Your conclusions match exactly with what is being experienced trying to use the RPi3 simultaneously as Client and AP. The iw command however does list this combination as valid. I wonder what testing and validation methods they used to declare that compatibility?

Code: Select all

pi@raspberrypi:~ $ iw list
.....
	software interface modes (can always be added):
	valid interface combinations:
		 * #{ managed } <= 1, #{ P2P-device } <= 1, #{ P2P-client, P2P-GO } <= 1,
		   total <= 3, #channels <= 1
		 * #{ managed } <= 1, #{ AP } <= 1, #{ P2P-client } <= 1, #{ P2P-device } <= 1,
		   total <= 4, #channels <= 1
.....
I will use a second USB wireless device in the Client mode while utilizing the on-board device as an AP. Thanks for your input. Much appreciated.

SurferTim
Posts: 1819
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: WiFi Client + AP

Thu Mar 01, 2018 1:18 pm

When you install another wireless device, you will probably have the same problem I had. The wlan devices don't always boot in the same order. Sometimes the onboard wlan is wlan0, and other times it is wlan1.

I use this to ensure each interface has the correct interface name assignment. Create file
/etc/udev/rules.d/72-static-name.rules
and insert these, changing the mac addresses to yours.

Code: Select all

 ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*",
ATTR{address}=="00:c0:ca:96:d8:8b", KERNEL=="w*",NAME="wlan0"

ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*",
ATTR{address}=="b8:27:eb:1e:56:52", KERNEL=="w*",NAME="wlan1"
Reboot.

TonyHuff
Posts: 5
Joined: Mon Feb 26, 2018 9:18 pm

Re: WiFi Client + AP

Thu Mar 01, 2018 9:26 pm

Excellent. That would have been a problem with the new configuration. Thank you.

Return to “Networking and servers”