scripts/netifd-wireless.sh: add support for specifying the operating band
[project/netifd.git] / scripts / netifd-wireless.sh
index a67e350d0c0ad5b49b6159d02fe3675ef5b2d005..b920296a9764ab61e513644c31694ab037668956 100644 (file)
@@ -15,11 +15,13 @@ add_driver() {
 
 wireless_setup_vif_failed() {
        local error="$1"
+       echo "Interface $_w_iface setup failed: $error"
 }
 
 wireless_setup_failed() {
        local error="$1"
 
+       echo "Device setup failed: $error"
        wireless_set_retry 0
 }
 
@@ -37,13 +39,12 @@ prepare_key_wep() {
 }
 
 _wdev_prepare_channel() {
-       json_get_vars channel hwmode
+       json_get_vars channel band hwmode
 
        auto_channel=0
        enable_ht=0
        htmode=
        hwmode="${hwmode##11}"
-       hwmode_n="${hwmode##n}"
 
        case "$channel" in
                ""|0|auto)
@@ -56,19 +57,8 @@ _wdev_prepare_channel() {
                ;;
        esac
 
-       [[ "$hwmode_n" = "$hwmode" ]] && {
-               enable_ht=1
-               hwmode="$hwmode_n"
-
-               json_get_vars htmode
-               case "$htmode" in
-                       HT20|HT40+|HT40-);;
-                       *) htmode= ;;
-               esac
-       }
-
        case "$hwmode" in
-               a|b|g) ;;
+               a|b|g|ad) ;;
                *)
                        if [ "$channel" -gt 14 ]; then
                                hwmode=a
@@ -77,6 +67,18 @@ _wdev_prepare_channel() {
                        fi
                ;;
        esac
+
+       case "$band" in
+               2g) hwmode=g;;
+               5g|6g|60g) hwmode=a;;
+               *)
+                       case "$hwmode" in
+                               *a) band=5g;;
+                               *ad) band=60g;;
+                               *b|*g) band=2g;;
+                       esac
+               ;;
+       esac
 }
 
 _wdev_handler() {
@@ -106,12 +108,13 @@ _wdev_wrapper() {
 
 _wdev_notify_init() {
        local command="$1"
-       local interface="$2"
+       local name="$2"
+       local value="$3"
 
        json_init
        json_add_int "command" "$command"
        json_add_string "device" "$__netifd_device"
-       [ -n "$interface" ] && json_add_string "interface" "$interface"
+       [ -n "$name" -a -n "$value" ] && json_add_string "$name" "$value"
        json_add_object "data"
 }
 
@@ -137,7 +140,17 @@ _wireless_add_vif() {
        local name="$1"; shift
        local ifname="$1"; shift
 
-       _wdev_notify_init $CMD_SET_DATA "$name"
+       _wdev_notify_init $CMD_SET_DATA "interface" "$name"
+       json_add_string "ifname" "$ifname"
+       _wdev_add_variables "$@"
+       _wdev_notify
+}
+
+_wireless_add_vlan() {
+       local name="$1"; shift
+       local ifname="$1"; shift
+
+       _wdev_notify_init $CMD_SET_DATA "vlan" "$name"
        json_add_string "ifname" "$ifname"
        _wdev_add_variables "$@"
        _wdev_notify
@@ -156,9 +169,14 @@ _wireless_set_data() {
 
 _wireless_add_process() {
        _wdev_notify_init $CMD_PROCESS_ADD
+       local exe="$2"
+       [ -L "$exe" ] && exe="$(readlink -f "$exe")"
        json_add_int pid "$1"
-       json_add_string exe "$2"
+       json_add_string exe "$exe"
        [ -n "$3" ] && json_add_boolean required 1
+       [ -n "$4" ] && json_add_boolean keep 1
+       exe2="$(readlink -f /proc/$1/exe)"
+       [ "$exe" != "$exe2" ] && echo "WARNING (wireless_add_process): executable path $exe does not match process $1 path ($exe2)"
        _wdev_notify
 }
 
@@ -176,6 +194,7 @@ _wireless_set_retry() {
 
 _wdev_wrapper \
        wireless_add_vif \
+       wireless_add_vlan \
        wireless_set_up \
        wireless_set_data \
        wireless_add_process \
@@ -189,15 +208,22 @@ wireless_vif_parse_encryption() {
        auth_mode_open=1
        auth_mode_shared=0
        auth_type=none
-       wpa_pairwise=CCMP
+
+       if [ "$hwmode" = "ad" ]; then
+               wpa_cipher="GCMP"
+       else
+               wpa_cipher="CCMP"
+       fi
+
        case "$encryption" in
-               *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_pairwise="CCMP TKIP";;
-               *aes|*ccmp) wpa_pairwise="CCMP";;
-               *tkip) wpa_pairwise="TKIP";;
+               *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_cipher="CCMP TKIP";;
+               *aes|*ccmp) wpa_cipher="CCMP";;
+               *tkip) wpa_cipher="TKIP";;
+               *gcmp) wpa_cipher="GCMP";;
        esac
 
        # 802.11n requires CCMP for WPA
-       [ "$enable_ht:$wpa_pairwise" = "1:TKIP" ] && wpa_pairwise="CCMP TKIP"
+       [ "$enable_ht:$wpa_cipher" = "1:TKIP" ] && wpa_cipher="CCMP TKIP"
 
        # Examples:
        # psk-mixed/tkip    => WPA1+2 PSK, TKIP
@@ -205,10 +231,10 @@ wireless_vif_parse_encryption() {
        # wpa2/tkip+aes     => WPA2 RADIUS, CCMP+TKIP
 
        case "$encryption" in
-               wpa2*|*psk2*)
+               wpa2*|wpa3*|*psk2*|psk3*|sae*|owe*)
                        wpa=2
                ;;
-               *mixed*)
+               wpa*mixed*|*psk*mixed*)
                        wpa=3
                ;;
                wpa*|*psk*)
@@ -216,11 +242,27 @@ wireless_vif_parse_encryption() {
                ;;
                *)
                        wpa=0
-                       wpa_pairwise=
+                       wpa_cipher=
                ;;
        esac
+       wpa_pairwise="$wpa_cipher"
 
        case "$encryption" in
+               owe*)
+                       auth_type=owe
+               ;;
+               wpa3-mixed*)
+                       auth_type=eap-eap192
+               ;;
+               wpa3*)
+                       auth_type=eap192
+               ;;
+               psk3-mixed*|sae-mixed*)
+                       auth_type=psk-sae
+               ;;
+               psk3*|sae*)
+                       auth_type=sae
+               ;;
                *psk*)
                        auth_type=psk
                ;;
@@ -240,14 +282,22 @@ wireless_vif_parse_encryption() {
                        esac
                ;;
        esac
+
+       case "$encryption" in
+               *osen*)
+                       auth_osen=1
+               ;;
+       esac
 }
 
-_get_vif_vars() {
-       # internal use
-       json_get_var _w_type mode
+_wireless_set_brsnoop_isolation() {
+       local multicast_to_unicast="$1"
+       local isolate
+
+       json_get_var isolate isolate
 
-       # for drivers
-       json_get_var network_bridge bridge
+       [ ${isolate:-0} -gt 0 -o -z "$network_bridge" ] && return
+       [ ${multicast_to_unicast:-1} -gt 0 ] && json_add_boolean isolate 1
 }
 
 for_each_interface() {
@@ -256,13 +306,18 @@ for_each_interface() {
        local _w_type
        local _w_found
 
+       local multicast_to_unicast
+
        json_get_keys _w_ifaces interfaces
        json_select interfaces
        for _w_iface in $_w_ifaces; do
                json_select "$_w_iface"
                if [ -n "$_w_types" ]; then
+                       json_get_var network_bridge bridge
+                       json_get_var multicast_to_unicast multicast_to_unicast
                        json_select config
-                       _get_vif_vars
+                       _wireless_set_brsnoop_isolation "$multicast_to_unicast"
+                       json_get_var _w_type mode
                        json_select ..
                        _w_types=" $_w_types "
                        [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
@@ -276,12 +331,50 @@ for_each_interface() {
        json_select ..
 }
 
+for_each_vlan() {
+       local _w_vlans _w_vlan
+
+       json_get_keys _w_vlans vlans
+       json_select vlans
+       for _w_vlan in $_w_vlans; do
+               json_select "$_w_vlan"
+               json_select config
+               "$@" "$_w_vlan"
+               json_select ..
+               json_select ..
+       done
+       json_select ..
+}
+
+for_each_station() {
+       local _w_stas _w_sta
+
+       json_get_keys _w_stas stas
+       json_select stas
+       for _w_sta in $_w_stas; do
+               json_select "$_w_sta"
+               json_select config
+               "$@" "$_w_sta"
+               json_select ..
+               json_select ..
+       done
+       json_select ..
+}
+
 _wdev_common_device_config() {
-       config_add_string channel hwmode
+       config_add_string channel hwmode band htmode noscan
 }
 
 _wdev_common_iface_config() {
-       config_add_string mode ssid encryption key
+       config_add_string mode ssid encryption 'key:wpakey'
+}
+
+_wdev_common_vlan_config() {
+       config_add_string name vid iface
+}
+
+_wdev_common_station_config() {
+       config_add_string mac key vid iface
 }
 
 init_wireless_driver() {
@@ -291,6 +384,8 @@ init_wireless_driver() {
        case "$cmd" in
                dump)
                        add_driver() {
+                               eval "drv_$1_cleanup"
+
                                json_init
                                json_add_string name "$1"
 
@@ -304,6 +399,16 @@ init_wireless_driver() {
                                eval "drv_$1_init_iface_config"
                                json_close_array
 
+                               json_add_array vlan
+                               _wdev_common_vlan_config
+                               eval "drv_$1_init_vlan_config"
+                               json_close_array
+
+                               json_add_array station
+                               _wdev_common_station_config
+                               eval "drv_$1_init_station_config"
+                               json_close_array
+
                                json_dump
                        }
                ;;