# Connecting the device to a wifi behind a captive

**URL:** <https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243>\
**Category:** balenaOS\
**Tags:** raspberrypi3\
**Created:** [February 23, 2020, 12:44pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243 "2020-02-23T12:44:09Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 9, 2020, 7:19am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/39 "2020-06-09T07:19:36Z")

</div>

@majorz , i want to update shortly.  
the solution seems to work, but.  
the issues:  
when a WIFI\_AP is created, when the device is up, the network manager starts the WIFI\_AP automatically, but this time using wlan0, and then i’m unable to add vap0 using iw because wlan0 is active - the workaround for this is deleting WIFI\_AP and restarting it after adding vap0, with the right interface.

this also makes me to have a service using network\_mode host for adding the vap0, and now i cant communicate with this service from services that are not on host mode.

do you have a suggestion how to approach this?

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 9, 2020, 7:59am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/40 "2020-06-09T07:59:25Z")

</div>

You can try setting interface-name=vap0 in the NetworkManager profile. autoconnect=false could be of help as well. Please let me know whether that will work for you. Can you also please explain more about the second question you have for communicating between those services - what you are trying to achieve more specifically?

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 9, 2020, 10:34am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/43 "2020-06-09T10:34:57Z")

</div>

My goal is to make vap0 available all the time, i couldnt do it through UDEV rules, so my container (connectionservice) is on network\_mode host, and it is doing on startup iw dev wlan0 interface add vap0 type \_\_ap addr 12:34:56:78:ab:cd  
i would like to avoid him having to be on host mode, and have the vap0 constant( UDEV rules didnt work for me)

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 9, 2020, 11:16am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/44 "2020-06-09T11:16:16Z")

</div>

i’ve added this to UDEV:  
“os”: {  
“udevRules”: {  
“12”: “ACTION==“add”, SUBSYSTEM==“net”, ENV{INTERFACE}==“wlan0”, RUN+=“iw dev wlan0 interface add vap0 type \_\_ap addr 12:34:56:78:ab:cd””  
}  
},

the functionality i need basically, is having vap0 always available on startup, before the containers start.

this is the output of the udev:  
root@b7cc5d3:~# udevadm test /sys/class/net/wlan0  
calling: test  
version 237  
This program is for debugging only, it does not run any program  
specified by a RUN key. It may show incorrect results, because  
some values may be different, or not available at a simulation run.

Load module index  
Skipping empty file: /etc/systemd/network/99-default.link  
Created link configuration context.  
Reading rules file: /etc/udev/rules.d/10-camera1-a.rules  
Reading rules file: /etc/udev/rules.d/10-camera1-c.rules  
Reading rules file: /etc/udev/rules.d/10-camera2-a.rules  
Reading rules file: /etc/udev/rules.d/10-camera2-c.rules  
Reading rules file: /lib/udev/rules.d/10-local-bt-hci-up.rules  
Reading rules file: /etc/udev/rules.d/12.rules  
Reading rules file: /lib/udev/rules.d/40-usb\_modeswitch.rules  
Reading rules file: /lib/udev/rules.d/49-teensy.rules  
Reading rules file: /lib/udev/rules.d/50-firmware.rules  
Reading rules file: /lib/udev/rules.d/50-udev-default.rules  
Specified group ‘render’ unknown  
Specified group ‘kvm’ unknown  
Reading rules file: /lib/udev/rules.d/60-block.rules  
Reading rules file: /lib/udev/rules.d/60-cdrom\_id.rules  
Reading rules file: /lib/udev/rules.d/60-drm.rules  
Reading rules file: /lib/udev/rules.d/60-evdev.rules  
Reading rules file: /lib/udev/rules.d/60-input-id.rules  
Reading rules file: /lib/udev/rules.d/60-persistent-alsa.rules  
Reading rules file: /lib/udev/rules.d/60-persistent-input.rules  
Reading rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules  
Reading rules file: /lib/udev/rules.d/60-persistent-storage.rules  
Reading rules file: /lib/udev/rules.d/60-persistent-v4l.rules  
Reading rules file: /lib/udev/rules.d/60-resin-update-state.rules  
Reading rules file: /lib/udev/rules.d/60-sensor.rules  
Reading rules file: /lib/udev/rules.d/60-serial.rules  
Reading rules file: /lib/udev/rules.d/64-btrfs.rules  
Reading rules file: /lib/udev/rules.d/70-joystick.rules  
Reading rules file: /lib/udev/rules.d/70-mouse.rules  
Reading rules file: /lib/udev/rules.d/70-power-switch.rules  
Reading rules file: /lib/udev/rules.d/70-touchpad.rules  
Reading rules file: /lib/udev/rules.d/70-uaccess.rules  
Reading rules file: /lib/udev/rules.d/71-seat.rules  
Reading rules file: /lib/udev/rules.d/73-seat-late.rules  
Reading rules file: /lib/udev/rules.d/75-net-description.rules  
Reading rules file: /lib/udev/rules.d/75-probe\_mtd.rules  
Reading rules file: /lib/udev/rules.d/77-mm-cinterion-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-dell-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-dlink-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-ericsson-mbm.rules  
Reading rules file: /lib/udev/rules.d/77-mm-fibocom-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-haier-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-huawei-configuration.rules  
Reading rules file: /lib/udev/rules.d/77-mm-huawei-net-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-longcheer-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-mtk-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-nokia-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-pcmcia-device-blacklist.rules  
Reading rules file: /lib/udev/rules.d/77-mm-quectel-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-sierra.rules  
Reading rules file: /lib/udev/rules.d/77-mm-simtech-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-telit-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-tplink-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-ublox-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-usb-device-blacklist.rules  
Reading rules file: /lib/udev/rules.d/77-mm-usb-serial-adapters-greylist.rules  
Reading rules file: /lib/udev/rules.d/77-mm-x22x-port-types.rules  
Reading rules file: /lib/udev/rules.d/77-mm-zte-port-types.rules  
Reading rules file: /lib/udev/rules.d/78-sound-card.rules  
Reading rules file: /lib/udev/rules.d/80-drivers.rules  
Reading rules file: /lib/udev/rules.d/80-mm-candidate.rules  
Reading rules file: /lib/udev/rules.d/80-net-setup-link.rules  
Reading rules file: /lib/udev/rules.d/84-nm-drivers.rules  
Reading rules file: /lib/udev/rules.d/85-nm-unmanaged.rules  
Reading rules file: /lib/udev/rules.d/85-regulatory.rules  
Reading rules file: /lib/udev/rules.d/90-nm-thunderbolt.rules  
Reading rules file: /lib/udev/rules.d/90-vconsole.rules  
Reading rules file: /lib/udev/rules.d/97-hid2hci.rules  
Reading rules file: /lib/udev/rules.d/99-misc.rules  
Reading rules file: /lib/udev/rules.d/99-systemd.rules  
Reading rules file: /lib/udev/rules.d/touchscreen.rules  
rules contain 98304 bytes tokens (8192 \* 12 bytes), 18749 bytes strings  
8389 strings (81430 bytes), 6971 de-duplicated (64100 bytes), 1419 trie nodes used  
RUN ‘iw dev wlan0 interface add vap0 type \_\_ap addr 12:34:56:78:ab:cd’ /etc/udev/rules.d/12.rules:1  
IMPORT builtin ‘net\_id’ /lib/udev/rules.d/75-net-description.rules:6  
IMPORT builtin ‘path\_id’ /lib/udev/rules.d/80-net-setup-link.rules:5  
IMPORT builtin ‘net\_setup\_link’ /lib/udev/rules.d/80-net-setup-link.rules:9  
No matching link configuration found.  
RUN ‘/lib/systemd/systemd-sysctl --prefix=/net/ipv4/conf/$name --prefix=/net/ipv4/neigh/$name --prefix=/net/ipv6/conf/$name --prefix=/net/ipv6/neigh/$name’ /lib/udev/rules.d/99-systemd.rules:60  
ACTION=add  
DEVPATH=/devices/platform/30b40000.mmc/mmc\_host/mmc0/mmc0:0001/mmc0:0001:1/net/wlan0  
DEVTYPE=wlan  
ID\_MM\_CANDIDATE=1  
ID\_NET\_DRIVER=brcmfmac  
ID\_PATH=platform-30b40000.mmc  
ID\_PATH\_TAG=platform-30b40000\_mmc  
IFINDEX=4  
INTERFACE=wlan0  
SUBSYSTEM=net  
SYSTEMD\_ALIAS=/sys/subsystem/net/devices/wlan0  
TAGS=:systemd:  
USEC\_INITIALIZED=8450378  
run: ‘iw dev wlan0 interface add vap0 type \_\_ap addr 12:34:56:78:ab:cd’  
run: ‘/lib/systemd/systemd-sysctl --prefix=/net/ipv4/conf/wlan0 --prefix=/net/ipv4/neigh/wlan0 --prefix=/net/ipv6/conf/wlan0 --prefix=/net/ipv6/neigh/wlan0’  
Unload module index  
Unloaded link configuration context.

---

<div class="post-metadata">

**Author:** ![thgreasi](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/thgreasi/32/454_2.png) [@thgreasi](https://forums.balena.io/u/thgreasi)\
**Post date:** [June 9, 2020, 1:36pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/45 "2020-06-09T13:36:57Z")

</div>

Hi @mellerdaniel ,  
Can you also clarify that the inner quotes of the configuration were properly escaped?  
I mean something like:

```auto
"os": {
  "udevRules": {
    "12": "ACTION==\"add\", SUBSYSTEM==\"net\", ENV{INTERFACE}==\"wlan0\", RUN+=\"iw dev wlan0 interface add vap0 type __ap addr 12:34:56:78:ab:cd\""
  }
}

```

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 9, 2020, 1:57pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/48 "2020-06-09T13:57:41Z")

</div>

If that does not work also try making the rule with a higher number, e.g. 99, not 12, so that all other rules have been run at the time it is executed.

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 16, 2020, 12:26pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/49 "2020-06-16T12:26:38Z")

</div>

tried with 99, not working.  
i now have a service that runs iw dev wlan0 interface add vap0 type \_\_ap addr 12:34:56:78:ab:cd and adds a AP with dbus API (python).  
that works fine, BUT 😕  
it causes conflict with a WIFI that was already connected, and it makes that connection not work.  
i will add also, that sometimes when the system starts, the network manager thinks that hot spot should be used by wlan0, and actually deletes all the other connections that were connected to my home router.  
so it cant automatically reconnect to defined routers

---

<div class="post-metadata">

**Author:** ![lucianbuzzo](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/lucianbuzzo/32/615_2.png) [@lucianbuzzo](https://forums.balena.io/u/lucianbuzzo)\
**Post date:** [June 16, 2020, 2:43pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/52 "2020-06-16T14:43:53Z")

</div>

Hi, some more log information could help debug what is happening - could you try accessing the device and running `udevadm monitor` to capture log output, and then in another tab run `udevadm trigger` to replay the system events. Once you’ve done this, please post the output of `udevadm monitor` here and hopefully this will provide more insight into the problem.

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 16, 2020, 2:52pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/53 "2020-06-16T14:52:03Z")

</div>

i have a suspect about the way i connect to a SSID using the python dbus API.

i use this example:  
[https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/python/dbus/add-wifi-psk-connection.py](https://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/python/dbus/add-wifi-psk-connection.py)

with the addition of ‘interface-name’ : ‘wlan0’ in the connection.

when i use just nmcli dev wifi connect SSID password PASS it looks like it works perfectly and acts as expected.

---

<div class="post-metadata">

**Author:** ![anujdeshpande](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/anujdeshpande/32/3155_2.png) [@anujdeshpande](https://forums.balena.io/u/anujdeshpande)\
**Post date:** [June 17, 2020, 8:40am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/54 "2020-06-17T08:40:01Z")

</div>

Hi Daniel

Thanks for sharing the example link.  
Can you share the log information that lucian requested above?

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 21, 2020, 11:06am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/56 "2020-06-21T11:06:41Z")

</div>

@majorz maybe you know the answer from your experience.  
We are trying to achieve that our device will serve as a hotspot, and phones will connect the device, but we want to make sure that the devices will keep using their 4g and use the internet from our device.  
is that easy to get using network manager? i’ve googled quite a lot and didnt find a solution yet.

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 22, 2020, 11:34am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/58 "2020-06-22T11:34:45Z")

</div>

So when a phone connects to the access point of the device, it will receive an IP address from dnsmasq. You would like the 4G Internet of the phone to be shared through that WiFi connection with the device. This is kind of reversed case of the usual share internet through a hotspot. While not possible with NetworkManager directly, I think you can achieve that in the following way:

Use the dnsmasq DBus API to find out what the assigned phone address is: [https://github.com/liquidm/dnsmasq/blob/master/dbus/DBus-interface#L266-L282](https://github.com/liquidm/dnsmasq/blob/master/dbus/DBus-interface#L266-L282).

Add a default route with `ip route add default via` to this IP address.

Both of those should be enough for the device to try to reach the Internet through the phone.

Next will be setting up the phone to share the 4G internet through the WiFi without creating a hotspot as the default Internet sharing involves creating a hotspot. I do not have experience those phone APIs and I am not sure how easy that would be.

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 22, 2020, 11:37am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/60 "2020-06-22T11:37:37Z")

</div>

> [@majorz](#):
>
> it will receive an IP address from dnsmasq. You would like the 4G Internet of the phone to be shared through that WiFi connection with the device. This is kind of reversed case of the usual share internet through a hotspot. While not possible with NetworkManager directly, I think you can achieve that in the following way:
> 
> Use the dnsmasq DBus API to find out what the assigned phone address is:

i actually was looking for something more simple, sorry for the misunderstanding.  
i’m looking for a way to make the phone, not using the device internet.  
our device is connected through Ethernet and also serves as a hotspot.  
my goal is that the internet of the mobile phone will keep being used by 4g, and it will be connected to the wifi of the device (so they can talk to each other)

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 22, 2020, 11:55am UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/61 "2020-06-22T11:55:10Z")

</div>

Ah, so when the phone connects to the WiFi hotspot, it will detect the captive portal, and will expect this to be the Internet connection with higher priority, so it will stop using the 4G connection. You would like to continue using the 4G Internet from the cellular connection on your phone.

This again falls into the responsibility of the phone OS. What you need is to set somehow the phone to use higher route metrics (lower priority) for routes that are behind a captive portal. NetworkManager has a connectivity check feature, which can do that, and you will need to see whether the phone OS has a corresponding feature which could be configured.

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 22, 2020, 12:05pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/62 "2020-06-22T12:05:38Z")

</div>

Actually what you can do from the device side is to disable the captive portal feature on top of the hotspot, and also tell dnsmasq to not push a gateway record to the phone through DHCP (this I think could be done through --dhcp-option). This way the phone will know that the device does not provide Internet access, so it will not create a default route for reaching the Internet. But then again the captive portal feature will have to be disabled, so when you connect from your phone to the device, you will not have the automatic pop-up displayed, so you will have to open the browser manually and enter the IP address of the device to reach it. If you are using some phone app for reaching the phone and not rely on the phone captive portal detection and auto pop-up display, then this will fit your use-case well.

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 22, 2020, 12:12pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/64 "2020-06-22T12:12:40Z")

</div>

> [@majorz](#):
>
> –dhcp-option). This way the phone will know that the device does not provide Internet access, so it will not create a default route for reaching the Internet. But then again the captive portal feature will have to be disabled, so when you connect from your phone to the device, you will not

this is exactly what i need.  
the solution without regards to the captive or not, we want this to be our permanent solution for every phone that will connect to our device.

how can this be done technically using network manager?

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 22, 2020, 1:01pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/65 "2020-06-22T13:01:42Z")

</div>

This won’t involve NetworkManager, only dnsmasq. When you currently start dnsmasq, you probably do it with this parameter added: `--dhcp-option=option:router,<GATEWAY_IP>`. This defines a gateway IP that is passed to the phone when it acquires DHCP information from the device. Since we do not want the phone to receive a gateway then this option should be omitted. If that does not work, try omitting only the IP like so ``–dhcp-option=option:router`. You may check for more information on this parameter the dnsmasq man page as it had some quirks.

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 22, 2020, 1:36pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/67 "2020-06-22T13:36:58Z")

</div>

> [@majorz](#):
>
> do not want the phone to receive a gateway then this option should be omitted. If that does not work, try o

i actually dont start dnmsq, it is probably started by NetworkManager.  
i only use python-dbus API to start the wifi hot spot.

---

<div class="post-metadata">

**Author:** ![majorz](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/majorz/32/639_2.png) [@majorz](https://forums.balena.io/u/majorz)\
**Post date:** [June 22, 2020, 1:40pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/68 "2020-06-22T13:40:56Z")

</div>

How do you assign IP addresses to the connected phones then?

---

<div class="post-metadata">

**Author:** ![mellerdaniel](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@mellerdaniel](https://forums.balena.io/u/mellerdaniel)\
**Post date:** [June 22, 2020, 1:48pm UTC](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243/69 "2020-06-22T13:48:08Z")

</div>

> [@majorz](#):
>
> dnsmasq

its automaticly done by Network manager i guess.

[Previous page](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243.md?page=1)

[Next page](https://forums.balena.io/t/connecting-the-device-to-a-wifi-behind-a-captive/65243.md?page=3)
