etc/network.subr: enhance wlan initialization
authorDaniel Fojt <df@neosystem.org>
Fri, 29 May 2020 17:34:24 +0000 (19:34 +0200)
committerDaniel Fojt <df@neosystem.org>
Tue, 2 Jun 2020 05:41:08 +0000 (07:41 +0200)
Introduce new functions wlan_get_unused() and wlan_is_parent(),
and extend wlan_up() to utilize them.

With this change, devices configured the traditional way in rc.conf(5)
are created first, and then all other wireless interfaces are assigned
to next available wlan<index> devices automatically.

This allows either to have wlan configured manually in rc.conf(5) via
"wlans_<iface>" or to remove the static assignment and let devices be
auto-created. With dhcpcd_enable and wpa_supplicant_enable in rc.conf,
no further configuration is then needed to get network connectivity.

Furthermore, this will allow to enhance devd(8) default configuration,
so that also hot-pluggable wireless interfaces are automatically detected
and brought up (ie. assigned to wlan<index> devices).

Reviewed by: Aaron LI

etc/network.subr

index e382e2b..b8b663a 100644 (file)
@@ -414,6 +414,32 @@ ifscript_down()
        return 1
 }
 
+# wlan_get_unused
+# walk through net.wlan and find unused device that can be created
+#
+wlan_get_unused()
+{
+       local idx
+
+       idx=0
+
+       while : ; do
+               if ! ${SYSCTL_N} -q net.wlan.${idx}.%parent >/dev/null; then
+                       echo "wlan${idx}"
+                       break
+               fi
+               idx=$((${idx} + 1))
+       done
+}
+
+# wlan_is_parent
+# check if given interface is parent for any existing wlan device
+#
+wlan_is_parent()
+{
+       sysctl -q net.wlan | grep -q "%parent: ${1}"
+}
+
 # wlan_up
 #      Create IEEE 802.11 interfaces.
 #
@@ -423,9 +449,31 @@ wlan_up()
        _prefix=
        _list=
 
+       local _rcconf _auto
+       _rcconf=""
+       _auto=""
+
+       # Order detected devices so that interfaces configured via rc.conf are
+       # created first, and then all other devices are automatically assigned
        for parent in `${SYSCTL_N} -q net.wlan.devices`; do
+               child_wlans=`get_if_var $parent wlans_IF`
+               if [ -n "${child_wlans}" ]; then
+                       _rcconf="${_rcconf} ${parent}"
+               else
+                       _auto="${_auto} ${parent}"
+               fi
+       done
+
+       for parent in ${_rcconf} ${_auto}; do
+               if wlan_is_parent $parent; then
+                       continue
+               fi
                # Parse wlans_$parent="$child ..."
                child_wlans=`get_if_var $parent wlans_IF`
+               # Or find first unused wlan device to create
+               if [ -z "${child_wlans}" ]; then
+                       child_wlans=`wlan_get_unused`
+               fi
                for child in ${child_wlans}; do
                        if ifexists $child; then
                                continue