How to use a single wireless adapter for both an access point and client on Raspberry Pi

dhcp-serverhostapdifconfigvlanwireless-networking

I have been able to create a wireless access point on my Raspberry Pi using a USB Wi-Fi dongle using hostapd on wlan0. What I would like to be able to do is have the access point be on some virtual interface (i.e. wlan0:1) or vlan (wlan0.123) and have wlan0 connect to an existing access point.

That way I can connect to the device via the access point it provides, or through the network that it was able to connect to.

The end goal is this: I can connect to the device using the access point it provides. It will then do a scan of wireless access points it finds, ask me which one I would like to connect to, and then attempt to connect to it. It can then tell me the IP address it was able to get on that new connection or tell me that it failed to connect (because I will still be connected via the access point it is providing).

If there is some other way to do this (without having two physical Wi-Fi adapters) I'm all ears.

For now, the steps I am taking are to get the AP working on the vlan interface. That is not workin… the AP is visible, but it seems that the DHCP server is not issuing an IP address.

Here are the contents of /etc/network/interfaces:

auto lo
auto wlan0 wlan0.10
auto eth0

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0

iface wlan0.10 inet static
  address 192.168.50.1
  netmask 255.255.255.0
  network 192.168.50.0
  broadcast 192.168.50.255
  gateway 192.168.50.1
  vlan_raw_device wlan0

iface wlan0 inet manual

Here are the contents of hostapd.conf:

interface=wlan0
driver=rtl871xdrv
country_code=NZ
ctrl_interface=wlan0.10
ctrl_interface_group=0
ssid=RPiAP
hw_mode=g
channel=1
wpa=3
wpa_passphrase=PASSWORD
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
macaddr_acl=0
wmm_enabled=1
eap_reauth_period=360000000

Note that above I had to say the interface is wlan0. It seems hostapd does not like it if I put wlan0.10.

Finally here is my dhcp configuration (isc-dhcp-server):

shared-network VLAN10 {
subnet 192.168.50.0 netmask 255.255.255.0 {
       range 192.168.50.10 192.168.50.250;
       option broadcast-address 192.168.50.255;
       option routers 192.168.50.1;
       default routers 192.168.50.1;
       default-lease-time 600;
       max-lease-time 7200;
       option domain-name "local";
}
}

It appears that the AP/dhcp doesn't like being on a VLAN as my phone seems to get stuck on "Obtaining IP address from RPiAP…". Or maybe I have to do some iptables rules to remove the tagging?

Best Answer

Using a single WLAN device both as access points (AP) and client (station, STA) is only possible if the device supports it. You can use iw phy or iw list to find out if your device does; there's a line valid interface combinations which describes what combinations are possible (including other mode). Details are for example here.

If your device supports it, you can add new virtual interfaces with something like

iw phy phy0 interface add wlan0_ap type ap

(modify as needed, the other type is sta for client/station).

Then you can run hostapd on one interface, and use the other interface normally.

Related Question