Wifi Hotspot using libnm (NetworkManager)

Hi,

I am trying to create a wifi hotspot using C++ code and the libnm-dev library. The following is the code I am using to publish the hotspot.

static void add_and_activate_cb(GObject* client, GAsyncResult* result, gpointer user_data) {
    GMainLoop* loop = (GMainLoop*)user_data;
    GError* error = NULL;
    NMActiveConnection* active_connection = nm_client_add_and_activate_connection_finish(NM_CLIENT(client), result, &error);

    if (active_connection) {
        g_object_unref(active_connection);
    }
    else {
        std::cerr << "Error creating hotspot: " << error->message << std::endl;
        g_error_free(error);
    }

    // Stop the main loop
    if (loop && g_main_loop_is_running(loop)) {
        g_main_loop_quit(loop);
    }
}

// Create a Wi-Fi hotspot with the given SSID and passphrase
void WiFi::createHotspot(const std::string& ssid, const std::string& passphrase, GMainLoop* loop)
{

    NMConnection* connection = nm_simple_connection_new();

    NMSettingConnection* s_con = NM_SETTING_CONNECTION(nm_setting_connection_new());

    g_object_set(G_OBJECT(s_con),
        NM_SETTING_CONNECTION_ID, "Hotspot",
        NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME,
        NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
        NULL);

    nm_connection_add_setting(connection, NM_SETTING(s_con));

    NMSettingWireless* s_wifi = NM_SETTING_WIRELESS(nm_setting_wireless_new());

    GBytes* ssid_bytes = g_bytes_new(ssid.data(), ssid.size());

    g_object_set(G_OBJECT(s_wifi),
        NM_SETTING_WIRELESS_SSID, ssid_bytes,
        NM_SETTING_WIRELESS_MODE, NM_SETTING_WIRELESS_MODE_AP,
        NULL);

    g_bytes_unref(ssid_bytes);

    nm_connection_add_setting(connection, NM_SETTING(s_wifi));

    NMSettingIPConfig* s_ip4 = NM_SETTING_IP_CONFIG(nm_setting_ip4_config_new());

    g_object_set(G_OBJECT(s_ip4),
        NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
        NULL);

    // Specify the static IP address for the hotspot
    nm_setting_ip_config_add_address(s_ip4, nm_ip_address_new(AF_INET, "10.42.0.1", 24, NULL));

    nm_connection_add_setting(connection, NM_SETTING(s_ip4));

    // NMSettingWirelessSecurity* s_wsec = NM_SETTING_WIRELESS_SECURITY(nm_setting_wireless_security_new());

    // g_object_set(G_OBJECT(s_wsec),
    //     NM_SETTING_WIRELESS_SECURITY_KEY_MGMT, "wpa-psk",
    //     NM_SETTING_WIRELESS_SECURITY_PSK, passphrase.c_str(),
    //     NULL);

    // nm_connection_add_setting(connection, NM_SETTING(s_wsec));

    nm_client_add_and_activate_connection_async(client_, connection, NM_DEVICE(wifi_device_), NULL, NULL, add_and_activate_cb, loop);

    g_object_unref(connection);

}

Current progress:

  1. Able to search for wifi networks and their properties,
  2. able to publish the hotspot with the requisite info but unable to connect to it.

There are a few errors that prop up, output of networkmanage journalctl is as follows:

Apr 25 13:56:24 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430984.9064] dhcp4 (ethernet0): state changed new lease, address=192.168.0.158
Apr 25 13:55:53 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430953.8101] device (vethd4e9e19): released from master device docker0
Apr 25 13:55:53 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430953.6927] manager: (veth9527c23): new Veth device (/org/freedesktop/NetworkManager/Devices/24)
Apr 25 13:52:43 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430763.3798] device (uap0): supplicant interface state: disconnected -> inactive
Apr 25 13:52:43 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430763.3756] device (uap0): supplicant interface state: inactive -> disconnected
Apr 25 13:52:43 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682430763.2797] device (uap0): set-hw-addr: failed to set MAC address to 86:46:5E:A6:86:59 (scanning) (NME_UNSPEC)
Apr 25 13:52:43 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682430763.2796] platform-linux: do-change-link[5]: failure changing link: failure 1 (Operation not permitted)
Apr 25 13:51:24 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430684.9061] dhcp4 (ethernet0): state changed new lease, address=192.168.0.158
Apr 25 13:46:24 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430384.9051] dhcp4 (ethernet0): state changed new lease, address=192.168.0.158
Apr 25 13:45:50 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430350.3757] device (uap0): supplicant interface state: interface_disabled -> inactive
Apr 25 13:45:50 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430350.2886] device (uap0): supplicant interface state: inactive -> interface_disabled
Apr 25 13:45:50 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682430350.2782] device (uap0): set-hw-addr: failed to set MAC address to F6:0E:16:F5:05:A7 (scanning) (NME_UNSPEC)
Apr 25 13:45:50 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682430350.2781] platform-linux: do-change-link[5]: failure changing link: failure 1 (Operation not permitted)
Apr 25 13:41:24 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682430084.9064] dhcp4 (ethernet0): state changed new lease, address=192.168.0.158
Apr 25 13:38:57 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429937.3792] device (uap0): supplicant interface state: disconnected -> inactive
Apr 25 13:38:57 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429937.3751] device (uap0): supplicant interface state: inactive -> disconnected
Apr 25 13:38:57 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682429937.2800] device (uap0): set-hw-addr: failed to set MAC address to 32:95:20:79:BB:3D (scanning) (NME_UNSPEC)
Apr 25 13:38:57 verdin-imx8mm-14756428 NetworkManager[595]: <warn>  [1682429937.2799] platform-linux: do-change-link[5]: failure changing link: failure 1 (Operation not permitted)
Apr 25 13:37:05 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429825.1585] device (mlan0): Activation: successful, device activated.
Apr 25 13:37:05 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429825.0609] device (mlan0): state change: secondaries -> activated (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:05 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429825.0601] device (mlan0): state change: ip-check -> secondaries (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.9866] device (mlan0): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.9786] device (mlan0): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.9773] device (mlan0): Activation: (wifi) Stage 2 of 5 (Device Configure) successful. Started Wi-Fi Hotspot "testNetwork"
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.9772] device (mlan0): supplicant interface state: disconnected -> completed
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.9371] device (mlan0): supplicant interface state: interface_disabled -> disconnected
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8256] device (mlan0): supplicant interface state: disconnected -> interface_disabled
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8248] Config: added 'key_mgmt' value 'NONE'
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8248] Config: added 'frequency' value '2412'
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8247] Config: added 'mode' value '2'
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8247] Config: added 'ssid' value 'testNetwork'
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8244] device (mlan0): Activation: (wifi) connection 'Hotspot' requires no security.  No secrets needed.
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8235] device (mlan0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.8218] device (mlan0): set-hw-addr: reset MAC address to 34:6F:24:31:32:F3 (preserve)
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.5348] device (mlan0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.5300] audit: op="connection-add-activate" uuid="98ce9356-77df-4348-9662-8bbd5f0e6ae6" name="Hotspot" pid=5305 uid=1000 result="success"
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.5296] device (mlan0): Activation: starting connection 'Hotspot' (98ce9356-77df-4348-9662-8bbd5f0e6ae6)
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.4923] audit: op="device-managed" interface="mlan0" ifindex=4 args="true" pid=5305 uid=1000 result="success"
Apr 25 13:37:04 verdin-imx8mm-14756428 NetworkManager[595]: <info>  [1682429824.4872] audit: op="radio-control" arg="wireless-enabled:on" pid=5305 uid=1000 result="success"

Output of dmesg is as follows:

[ 1532.339325] audit: type=1006 audit(1682429806.869:149): pid=5192 uid=0 old-auid=4294967295 auid=1000 tty=(none) old-ses=4294967295 ses=12 res=1
[ 1532.339342] audit: type=1300 audit(1682429806.869:149): arch=c00000b7 syscall=64 success=yes exit=4 a0=7 a1=fffff1c052a0 a2=4 a3=ffff8c4e9920 items=0 ppid=1 pid=5192 auid=1000 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=12 comm="sshd" exe="/usr/sbin/sshd" key=(null)
[ 1532.339353] audit: type=1327 audit(1682429806.869:149): proctitle=737368643A20746F72697A6F6E205B707269765D
[ 1533.274434] docker0: port 1(vethd4e9e19) entered blocking state
[ 1533.274511] docker0: port 1(vethd4e9e19) entered disabled state
[ 1533.276009] device vethd4e9e19 entered promiscuous mode
[ 1533.276970] audit: type=1700 audit(1682429807.809:150): dev=vethd4e9e19 prom=256 old_prom=0 auid=4294967295 uid=0 gid=0 ses=4294967295
[ 1533.277864] audit: type=1300 audit(1682429807.809:150): arch=c00000b7 syscall=206 success=yes exit=40 a0=f a1=4000df15f0 a2=28 a3=0 items=0 ppid=1 pid=729 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="dockerd" exe="/usr/bin/dockerd" key=(null)
[ 1533.277956] audit: type=1327 audit(1682429807.809:150): proctitle=2F7573722F62696E2F646F636B657264002D480066643A2F2F002D48007463703A2F2F3132372E302E302E313A32333735
[ 1533.397102] audit: type=1325 audit(1682429807.929:151): table=nat family=2 entries=15 op=xt_replace pid=5226 comm="iptables"
[ 1533.397535] audit: type=1300 audit(1682429807.929:151): arch=c00000b7 syscall=208 success=yes exit=0 a0=4 a1=0 a2=40 a3=aaaae1d6aff0 items=0 ppid=729 pid=5226 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-legacy-multi" key=(null)
[ 1533.397848] audit: type=1327 audit(1682429807.929:151): proctitle=2F7573722F7362696E2F69707461626C6573002D2D77616974002D74006E6174002D4100444F434B4552002D7000746370002D6400302F30002D2D64706F7274003439313536002D6A00444E4154002D2D746F2D64657374696E6174696F6E003137322E31372E302E323A363530320000002D6900646F636B657230
[ 1533.419944] audit: type=1325 audit(1682429807.949:152): table=filter family=2 entries=30 op=xt_replace pid=5228 comm="iptables"
[ 1534.144998] eth0: renamed from veth9527c23
[ 1534.182073] IPv6: ADDRCONF(NETDEV_CHANGE): vethd4e9e19: link becomes ready
[ 1534.182221] docker0: port 1(vethd4e9e19) entered blocking state
[ 1534.182231] docker0: port 1(vethd4e9e19) entered forwarding state
[ 1550.416973] IPv6: ADDRCONF(NETDEV_CHANGE): mlan0: link becomes ready
[ 1662.733809] mwifiex_sdio mmc2:0001:1: CMD_RESP: cmd 0x4d error, result=0x1
[ 1662.740506] mwifiex_sdio mmc2:0001:1: set mac address failed: ret=-1
[ 1706.119572] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 1766.108003] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 1772.151408] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 1930.578099] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 2075.741744] mwifiex_sdio mmc2:0001:1: CMD_RESP: cmd 0x4d error, result=0x1
[ 2075.741791] mwifiex_sdio mmc2:0001:1: set mac address failed: ret=-1
[ 2441.149329] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 2442.175809] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 2488.754776] mwifiex_sdio mmc2:0001:1: CMD_RESP: cmd 0x4d error, result=0x1
[ 2488.760273] mwifiex_sdio mmc2:0001:1: set mac address failed: ret=-1
[ 2594.688520] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf
[ 2679.024451] kauditd_printk_skb: 5 callbacks suppressed
[ 2679.024463] audit: type=1325 audit(1682430953.524:154): table=nat family=2 entries=17 op=xt_replace pid=5941 comm="iptables"
[ 2679.024914] audit: type=1300 audit(1682430953.524:154): arch=c00000b7 syscall=208 success=yes exit=0 a0=4 a1=0 a2=40 a3=aaab0b557520 items=0 ppid=729 pid=5941 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-legacy-multi" key=(null)
[ 2679.025235] audit: type=1327 audit(1682430953.524:154): proctitle=2F7573722F7362696E2F69707461626C6573002D2D77616974002D74006E6174002D4400444F434B4552002D7000746370002D6400302F30002D2D64706F7274003439313536002D6A00444E4154002D2D746F2D64657374696E6174696F6E003137322E31372E302E323A363530320000002D6900646F636B657230
[ 2679.034178] audit: type=1325 audit(1682430953.534:155): table=filter family=2 entries=31 op=xt_replace pid=5944 comm="iptables"
[ 2679.034571] audit: type=1300 audit(1682430953.534:155): arch=c00000b7 syscall=208 success=yes exit=0 a0=4 a1=0 a2=40 a3=aaaafacf15f0 items=0 ppid=729 pid=5944 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-legacy-multi" key=(null)
[ 2679.034856] audit: type=1327 audit(1682430953.534:155): proctitle=2F7573722F7362696E2F69707461626C6573002D2D77616974002D740066696C746572002D4400444F434B45520000002D6900646F636B657230002D6F00646F636B657230002D7000746370002D64003137322E31372E302E32002D2D64706F72740036353032002D6A00414343455054
[ 2679.044988] audit: type=1325 audit(1682430953.544:156): table=nat family=2 entries=16 op=xt_replace pid=5946 comm="iptables"
[ 2679.045401] audit: type=1300 audit(1682430953.544:156): arch=c00000b7 syscall=208 success=yes exit=0 a0=4 a1=0 a2=40 a3=aaab172af190 items=0 ppid=729 pid=5946 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="iptables" exe="/usr/sbin/xtables-legacy-multi" key=(null)
[ 2679.045710] audit: type=1327 audit(1682430953.544:156): proctitle=2F7573722F7362696E2F69707461626C6573002D2D77616974002D74006E6174002D4400504F5354524F5554494E47002D7000746370002D73003137322E31372E302E32002D64003137322E31372E302E32002D2D64706F72740036353032002D6A004D415351554552414445
[ 2679.055075] docker0: port 1(vethd4e9e19) entered disabled state
[ 2679.067072] veth9527c23: renamed from eth0
[ 2679.207441] docker0: port 1(vethd4e9e19) entered disabled state
[ 2679.209135] device vethd4e9e19 left promiscuous mode
[ 2679.209203] audit: type=1700 audit(1682430953.684:157): dev=vethd4e9e19 prom=0 old_prom=256 auid=4294967295 uid=0 gid=0 ses=4294967295
[ 2679.209485] docker0: port 1(vethd4e9e19) entered disabled state
[ 2798.126574] mwifiex_sdio mmc2:0001:1: do not receive mgmt frames on uninitialized intf

I believe there is an issue related to the permissions of my application container.

Can someone please take a look and advise?

Also, when working with bluetooth, I am unable to get a stable connection until a compat flag is added as per the following post:

maybe wifi has a similar caveat. dont know.

one more thing, if the hotspot is advertised with a password, a warning is raised by networkmanager about dnsmasq not being present.

Kindly advise.

Hi @geopaxpvtltd

Unfortunately, there are some known issues with using NM for creating hotspots. Specifically, the mentioned missing dnsmasq is one of them. At the moment creating hotspots with NM is considered unsupported but it is on our backlog.

Sorry, I don’t have a better answer.

Drew

Thank you for your response!

Is there a way for me to subscribe to your process on this to know when it will be sorted out?

Furthermore, I was reading the networking with torizoncore article where it was mentioned that a dhcp server needs to be configured to be able to use wifi ap mode.
My question is that do i need to configure that dhcp server manually when using my code to set up a wifi ap? Or is the network manager supposed to take care of it?

Lastly, is this issue networkmanager specific or its library specific? what happens if someone were to use connman?

Is there any other way to do it? if so, please guide.

Thank you for your help

You can subscribe here: TorizonCore Issue Tracker. You can click on the “Get Updates” button.

Right now we do not have this answer. You can try to replace NetworkManager with connman, but you would need to do it on Yocto: Build Torizon OS from Source With Yocto Project/OpenEmbedded | Toradex Developer Center. It might be not a straightforward change.

Best regards,

I have subscribed to the list.

I understand but I am not such an advanced developer. I chose torizoncore as a module for our proposed product just because all of this was maintained by the toradex and I had hoped basic functionality would be working. Anyway. Would you be able to give a rough timeline for this issue (a month or a few months)? I have not been able to find the issue in the issue tracker.

Can you confirm the issue is with AP mode only and station mode is working as expected?

Best Regards,
Hassan.

Hi @geopaxpvtltd !

By station mode, do you mean a simple client or maybe a bridge? As a simple client, it should work.

I need to check internally if we have some estimation.

Most probably the internal Issue Tracker was not synced yet with the public one.

Best regards,

A simple client only. Thanks. I will try and check if there is an issue.

Much appreciated.

You can view known issues here:

But that does not necessarily cover this as it is currently tracked as a feature. We are discussing how to make feature information more visible but that’s tricky as plans change and we don’t necessarily want to commit to things too early. The best option at the moment is to monitor release announcements here: Embedded Computing News

As for supported hotspots at the moment, hostapd is available in Torizon and I’ve heard reports of people using it successfully. The docs here are primarily for our Yocto releases but should help you use that as a mid-term solution.

HTH,
Drew

Thank you for the details, @drew.tx.

Yes, a simple client is working. I will try to make do with it at the moment but if you guys manage to get the hotspot issue resolved soon, that would be awesome. and a way to be notified when it is done.

In the meantime, I will also try the hostapd approach and let you know the results.

Thanks again.

Best Regards,
Hassan.

Hi @drew.tx !

It has been a long while since I came around to this issue.

In the issue tracker list it states a version of this error has been fixed:

Can you confirm if this fix applies to my situation?

Best Regards,
Hassan.

Hi @geopaxpvtltd I believe the fix is that we now have a documented method to use AP mode with the hostapd package. The NM docs are still there but there is a noted compatibility warning.

See this link for more details.

Drew

Hello @drew.tx Thank you for the reply.

Actually, I wanted to control this from the C++ program. I just wanted to know Is this issue going to be resolved by any upstream fix or is it a limitation of the hardware?

If it is a limitation of the hardware, can we implement any other wifi module on the carrier board that may help us resolve this and then we can use a verdin module without the wifi/bt tag?

I have been working to include an RTL8723DU using torizoncore builder. If the above is an option that you would suggest, we could also manage that.

Best Regards,
Hassan.

Hi @geopaxpvtltd

As near as I can tell it is an issue with Network Manager and not a hardware limitation. You can certainly add a different wifi chip on your carrier board but I don’t think that will help in this situation. Presumably at some point this will be fixed upstream and then eventually make its way back into our builds.

Drew

1 Like

Thank you for the clarification.

I was able to use hostapd for the ap and was able to control it from the application containers via scripts etc.