rc.d/netif: Rework wlan interface configuration
authorAaron LI <aly@aaronly.me>
Wed, 12 Dec 2018 03:52:24 +0000 (11:52 +0800)
committerAaron LI <aly@aaronly.me>
Wed, 12 Dec 2018 15:24:40 +0000 (23:24 +0800)
* wlan interfaces are more similar to clonable interfaces rather than
  vlan interfaces, so move the relevant code from childif_create() to
  network.subr as wlan_up(), which will be called to create the wlan
  interfaces before setting up them.

  Now, list_net_interfaces() shouldn't list "net.wlan.devices".

* Add the wlan_down() function in network.subr to destroy the wlan
  devices.

* Meanwhile, fix childif_destroy() to properly destroy the vlan
  interfaces created by childif_create().  Also tweak the order of this
  function in netif_stop().

* The 2-second delay in netif_start() doesn't apply anymore, since the
  DHCP and WPA configurations are now handled in ifconfig_up() called by
  ifn_start().

This patch is loosely based on FreeBSD's code.  Thanks to them.

etc/network.subr
etc/rc.d/netif

index e62b6b8..26d71f2 100644 (file)
@@ -406,6 +406,76 @@ ifscript_down()
        return 1
 }
 
+# wlan_up
+#      Create IEEE 802.11 interfaces.
+#
+wlan_up()
+{
+       local _prefix _list parent child child_wlans create_args debug_flags
+       _prefix=
+       _list=
+
+       for parent in `${SYSCTL_N} -q net.wlan.devices`; do
+               # Parse wlans_$parent="$child ..."
+               child_wlans=`get_if_var $parent wlans_IF`
+               for child in ${child_wlans}; do
+                       if ifexists $child; then
+                               continue
+                       fi
+
+                       create_args="wlandev $parent `get_if_var $child create_args_IF`"
+                       debug_flags="`get_if_var $child wlandebug_IF`"
+                       if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
+                               ifconfig $child create ${create_args}
+                       else
+                               ifconfig wlan create ${create_args} name $child
+                       fi
+                       if [ $? -eq 0 ]; then
+                               _list="${_list}${_prefix}${child}"
+                               [ -z "$_prefix" ] && _prefix=' '
+                       fi
+                       if [ -n "${debug_flags}" ]; then
+                               wlandebug -i $child ${debug_flags}
+                       fi
+               done
+       done
+
+       if [ -n "${_list}" ]; then
+               echo "Created wlan interfaces: ${_list}"
+       fi
+       debug "Created wlan interfaces: ${_list}"
+}
+
+# wlan_down
+#      Destroy IEEE 802.11 interfaces.
+#
+wlan_down()
+{
+       local _prefix _list parent child child_wlans
+       _prefix=
+       _list=
+
+       for parent in `${SYSCTL_N} -q net.wlan.devices`; do
+               child_wlans=`get_if_var $parent wlans_IF`
+               for child in ${child_wlans}; do
+                       if ! ifexists $child; then
+                               continue
+                       fi
+
+                       ifconfig -n $child destroy
+                       if [ $? -eq 0 ]; then
+                               _list="${_list}${_prefix}${child}"
+                               [ -z "$_prefix" ] && _prefix=' '
+                       fi
+               done
+       done
+
+       if [ -n "${_list}" ]; then
+               echo "Destroyed wlan interfaces: ${_list}"
+       fi
+       debug "Destroyed wlan interfaces: ${_list}"
+}
+
 # clone_up
 #      Create cloneable interfaces.
 #
@@ -508,7 +578,7 @@ list_net_interfaces()
 
        case ${network_interfaces} in
        [Aa][Uu][Tt][Oo])
-               _autolist="`ifconfig -l` `sysctl -in net.wlan.devices`"
+               _autolist=$(ifconfig -l)
                _lo=
                for _if in ${_autolist} ; do
                        if [ "$_if" = "lo0" ]; then
index 03c4f07..434038e 100644 (file)
@@ -37,7 +37,9 @@ start_cmd="netif_start"
 stop_cmd="netif_stop"
 cloneup_cmd="clone_up"
 clonedown_cmd="clone_down"
-extra_commands="cloneup clonedown"
+wlanup_cmd="wlan_up"
+wlandown_cmd="wlan_down"
+extra_commands="cloneup clonedown wlanup wlandown"
 _cmdifn=
 
 netif_start()
@@ -51,6 +53,9 @@ netif_start()
                # We're operating as a general network start routine.
                #
 
+               # Create IEEE 802.11 interfaces
+               wlan_up
+
                # Create cloned interfaces
                clone_up
 
@@ -63,10 +68,6 @@ netif_start()
 
        # Configure the interface(s).
        netif_common ifn_start verbose
-
-       # Give our interfaces a little time to come up
-       # before we start pounding them, e.g., DHCP client.
-       sleep 2
 }
 
 netif_stop()
@@ -134,7 +135,7 @@ netif_common()
                eval showstat=\$showstat_${ifn}
                if [ ! -z ${showstat} ]; then
                        if [ -n "$_verbose" ]; then
-                               ifconfig ${ifn} 2>/dev/null
+                               ifconfig ${ifn}
                        else
                                echo -n " ${ifn}"
                        fi
@@ -173,46 +174,24 @@ ifn_stop()
 
        [ -z "$ifn" ] && err 1 "ifn_stop called without an interface"
 
+       childif_destroy ${ifn} && cfg=0
        ifalias_down ${ifn} && cfg=0
        ifconfig_down ${ifn} && cfg=0
        ifscript_down ${ifn} && cfg=0
-       childif_destroy ${ifn} && cfg=0
 
        return $cfg
 }
 
-# childif_create
+# childif_create ifn
 #      Create and configure child interfaces.  Return 0 if child
 #      interfaces are created.
 #
 childif_create()
 {
-       local cfg child child_vlans child_wlans create_args debug_flags ifn i
+       local cfg child child_vlans create_args ifn i
        cfg=1
        ifn=$1
 
-       # Create wireless interfaces
-       child_wlans=`get_if_var $ifn wlans_IF`
-
-       for child in ${child_wlans}; do
-               create_args="wlandev $ifn `get_if_var $child create_args_IF`"
-               debug_flags="`get_if_var $child wlandebug_IF`"
-
-               if expr $child : 'wlan[0-9][0-9]*$' >/dev/null 2>&1; then
-                       ifconfig $child create ${create_args} && cfg=0
-                       if [ -n "${debug_flags}" ]; then
-                               wlandebug -i $child ${debug_flags}
-                       fi
-               else
-                       i=`ifconfig wlan create ${create_args}`
-                       if [ -n "${debug_flags}" ]; then
-                               wlandebug -i $i ${debug_flags}
-                       fi
-                       ifconfig $i name $child && cfg=0
-               fi
-               ifn_start $child
-       done
-
        # Create vlan interfaces
        child_vlans=`get_if_var $ifn vlans_IF`
 
@@ -236,17 +215,21 @@ childif_create()
        return ${cfg}
 }
 
-# childif_destroy
+# childif_destroy ifn
 #      Destroy child interfaces.
 #
 childif_destroy()
 {
-       local cfg child child_vlans child_wlans ifn
+       local cfg child child_vlans ifn
        cfg=1
+       ifn=$1
 
-       child_wlans=`get_if_var $ifn wlans_IF`
-       for child in ${child_wlans}; do
-               if ! `ifconfig -n $child > /dev/null 2>&1`; then
+       child_vlans=`get_if_var $ifn vlans_IF`
+       for child in ${child_vlans}; do
+               if expr $child : '[1-9][0-9]*$' >/dev/null 2>&1; then
+                       child="${ifn}.${child}"
+               fi
+               if ! ifexists $child; then
                        continue
                fi
                ifn_stop $child