| 1 | #!/bin/sh |
| 2 | append DRIVERS "mac80211" |
| 3 | |
| 4 | mac80211_hostapd_setup_base() { |
| 5 | local phy="$1" |
| 6 | local ifname="$2" |
| 7 | |
| 8 | cfgfile="/var/run/hostapd-$phy.conf" |
| 9 | config_get device "$vif" device |
| 10 | config_get country "$device" country |
| 11 | config_get hwmode "$device" hwmode |
| 12 | config_get channel "$device" channel |
| 13 | config_get_bool noscan "$device" noscan |
| 14 | [ -n "$channel" -a -z "$hwmode" ] && wifi_fixup_hwmode "$device" |
| 15 | [ "$channel" = auto ] && channel= |
| 16 | [ -n "$hwmode" ] && { |
| 17 | config_get hwmode_11n "$device" hwmode_11n |
| 18 | [ -n "$hwmode_11n" ] && { |
| 19 | hwmode="$hwmode_11n" |
| 20 | append base_cfg "ieee80211n=1" "$N" |
| 21 | config_get htmode "$device" htmode |
| 22 | config_get ht_capab_list "$device" ht_capab |
| 23 | case "$htmode" in |
| 24 | HT20|HT40+|HT40-) ht_capab="[$htmode]";; |
| 25 | *)ht_capab=;; |
| 26 | esac |
| 27 | for cap in $ht_capab_list; do |
| 28 | ht_capab="$ht_capab[$cap]" |
| 29 | done |
| 30 | [ -n "$ht_capab" ] && append base_cfg "ht_capab=$ht_capab" "$N" |
| 31 | } |
| 32 | } |
| 33 | cat > "$cfgfile" <<EOF |
| 34 | ctrl_interface=/var/run/hostapd-$phy |
| 35 | driver=nl80211 |
| 36 | wmm_ac_bk_cwmin=4 |
| 37 | wmm_ac_bk_cwmax=10 |
| 38 | wmm_ac_bk_aifs=7 |
| 39 | wmm_ac_bk_txop_limit=0 |
| 40 | wmm_ac_bk_acm=0 |
| 41 | wmm_ac_be_aifs=3 |
| 42 | wmm_ac_be_cwmin=4 |
| 43 | wmm_ac_be_cwmax=10 |
| 44 | wmm_ac_be_txop_limit=0 |
| 45 | wmm_ac_be_acm=0 |
| 46 | wmm_ac_vi_aifs=2 |
| 47 | wmm_ac_vi_cwmin=3 |
| 48 | wmm_ac_vi_cwmax=4 |
| 49 | wmm_ac_vi_txop_limit=94 |
| 50 | wmm_ac_vi_acm=0 |
| 51 | wmm_ac_vo_aifs=2 |
| 52 | wmm_ac_vo_cwmin=2 |
| 53 | wmm_ac_vo_cwmax=3 |
| 54 | wmm_ac_vo_txop_limit=47 |
| 55 | wmm_ac_vo_acm=0 |
| 56 | tx_queue_data3_aifs=7 |
| 57 | tx_queue_data3_cwmin=15 |
| 58 | tx_queue_data3_cwmax=1023 |
| 59 | tx_queue_data3_burst=0 |
| 60 | tx_queue_data2_aifs=3 |
| 61 | tx_queue_data2_cwmin=15 |
| 62 | tx_queue_data2_cwmax=63 |
| 63 | tx_queue_data2_burst=0 |
| 64 | tx_queue_data1_aifs=1 |
| 65 | tx_queue_data1_cwmin=7 |
| 66 | tx_queue_data1_cwmax=15 |
| 67 | tx_queue_data1_burst=3.0 |
| 68 | tx_queue_data0_aifs=1 |
| 69 | tx_queue_data0_cwmin=3 |
| 70 | tx_queue_data0_cwmax=7 |
| 71 | tx_queue_data0_burst=1.5 |
| 72 | ${hwmode:+hw_mode=$hwmode} |
| 73 | ${channel:+channel=$channel} |
| 74 | ${country:+country_code=$country} |
| 75 | ${noscan:+noscan=$noscan} |
| 76 | $base_cfg |
| 77 | |
| 78 | EOF |
| 79 | } |
| 80 | |
| 81 | mac80211_hostapd_setup_bss() { |
| 82 | local phy="$1" |
| 83 | local vif="$2" |
| 84 | |
| 85 | hostapd_cfg= |
| 86 | cfgfile="/var/run/hostapd-$phy.conf" |
| 87 | config_get ifname "$vif" ifname |
| 88 | |
| 89 | if [ -f "$cfgfile" ]; then |
| 90 | append hostapd_cfg "bss=$ifname" "$N" |
| 91 | else |
| 92 | mac80211_hostapd_setup_base "$phy" "$ifname" |
| 93 | append hostapd_cfg "interface=$ifname" "$N" |
| 94 | fi |
| 95 | |
| 96 | local net_cfg bridge |
| 97 | net_cfg="$(find_net_config "$vif")" |
| 98 | [ -z "$net_cfg" ] || bridge="$(bridge_interface "$net_cfg")" |
| 99 | config_set "$vif" bridge "$bridge" |
| 100 | |
| 101 | hostapd_set_bss_options hostapd_cfg "$vif" |
| 102 | |
| 103 | config_get_bool wds "$vif" wds 0 |
| 104 | [ "$wds" -gt 0 ] && append hostapd_cfg "wds_sta=1" "$N" |
| 105 | |
| 106 | config_get macaddr "$vif" macaddr |
| 107 | config_get_bool hidden "$vif" hidden 0 |
| 108 | config_get maxassoc "$vif" maxassoc |
| 109 | cat >> /var/run/hostapd-$phy.conf <<EOF |
| 110 | $hostapd_cfg |
| 111 | wmm_enabled=1 |
| 112 | bssid=$macaddr |
| 113 | ignore_broadcast_ssid=$hidden |
| 114 | ${maxassoc:+max_num_sta=$maxassoc} |
| 115 | EOF |
| 116 | } |
| 117 | |
| 118 | mac80211_start_vif() { |
| 119 | local vif="$1" |
| 120 | local ifname="$2" |
| 121 | |
| 122 | local net_cfg |
| 123 | net_cfg="$(find_net_config "$vif")" |
| 124 | [ -z "$net_cfg" ] || start_net "$ifname" "$net_cfg" |
| 125 | |
| 126 | set_wifi_up "$vif" "$ifname" |
| 127 | } |
| 128 | |
| 129 | find_mac80211_phy() { |
| 130 | local device="$1" |
| 131 | |
| 132 | local macaddr="$(config_get "$device" macaddr | tr 'A-Z' 'a-z')" |
| 133 | config_get phy "$device" phy |
| 134 | [ -z "$phy" -a -n "$macaddr" ] && { |
| 135 | for phy in $(ls /sys/class/ieee80211 2>/dev/null); do |
| 136 | [ "$macaddr" = "$(cat /sys/class/ieee80211/${phy}/macaddress)" ] || continue |
| 137 | config_set "$device" phy "$phy" |
| 138 | break |
| 139 | done |
| 140 | config_get phy "$device" phy |
| 141 | } |
| 142 | [ -n "$phy" -a -d "/sys/class/ieee80211/$phy" ] || { |
| 143 | echo "PHY for wifi device $1 not found" |
| 144 | return 1 |
| 145 | } |
| 146 | [ -z "$macaddr" ] && { |
| 147 | config_set "$device" macaddr "$(cat /sys/class/ieee80211/${phy}/macaddress)" |
| 148 | } |
| 149 | return 0 |
| 150 | } |
| 151 | |
| 152 | scan_mac80211() { |
| 153 | local device="$1" |
| 154 | local adhoc sta ap monitor mesh |
| 155 | |
| 156 | config_get vifs "$device" vifs |
| 157 | for vif in $vifs; do |
| 158 | config_get mode "$vif" mode |
| 159 | case "$mode" in |
| 160 | adhoc|sta|ap|monitor|mesh) |
| 161 | append $mode "$vif" |
| 162 | ;; |
| 163 | *) echo "$device($vif): Invalid mode, ignored."; continue;; |
| 164 | esac |
| 165 | done |
| 166 | |
| 167 | config_set "$device" vifs "${ap:+$ap }${adhoc:+$adhoc }${sta:+$sta }${monitor:+$monitor }${mesh:+$mesh}" |
| 168 | } |
| 169 | |
| 170 | list_phy_interfaces() { |
| 171 | local phy="$1" |
| 172 | if [ -d "/sys/class/ieee80211/${phy}/device/net" ]; then |
| 173 | ls "/sys/class/ieee80211/${phy}/device/net" 2>/dev/null; |
| 174 | else |
| 175 | ls "/sys/class/ieee80211/${phy}/device" 2>/dev/null | grep net: | sed -e 's,net:,,g' |
| 176 | fi |
| 177 | } |
| 178 | |
| 179 | disable_mac80211() ( |
| 180 | local device="$1" |
| 181 | |
| 182 | find_mac80211_phy "$device" || return 0 |
| 183 | config_get phy "$device" phy |
| 184 | |
| 185 | set_wifi_down "$device" |
| 186 | # kill all running hostapd and wpa_supplicant processes that |
| 187 | # are running on atheros/mac80211 vifs |
| 188 | for pid in `pidof hostapd`; do |
| 189 | grep -E "$phy" /proc/$pid/cmdline >/dev/null && \ |
| 190 | kill $pid |
| 191 | done |
| 192 | |
| 193 | include /lib/network |
| 194 | for wdev in $(list_phy_interfaces "$phy"); do |
| 195 | [ -f "/var/run/$wdev.pid" ] && kill $(cat /var/run/$wdev.pid) >&/dev/null 2>&1 |
| 196 | for pid in `pidof wpa_supplicant`; do |
| 197 | grep "$wdev" /proc/$pid/cmdline >/dev/null && \ |
| 198 | kill $pid |
| 199 | done |
| 200 | ifconfig "$wdev" down 2>/dev/null |
| 201 | unbridge "$dev" |
| 202 | iw dev "$wdev" del |
| 203 | done |
| 204 | |
| 205 | return 0 |
| 206 | ) |
| 207 | get_freq() { |
| 208 | local phy="$1" |
| 209 | local chan="$2" |
| 210 | iw "$phy" info | grep -E -m1 "(\* ${chan:-....} MHz${chan:+|\\[$chan\\]})" | grep MHz | awk '{print $2}' |
| 211 | } |
| 212 | enable_mac80211() { |
| 213 | local device="$1" |
| 214 | config_get channel "$device" channel |
| 215 | config_get vifs "$device" vifs |
| 216 | config_get txpower "$device" txpower |
| 217 | config_get country "$device" country |
| 218 | config_get distance "$device" distance |
| 219 | config_get frag "$device" frag |
| 220 | config_get rts "$device" rts |
| 221 | find_mac80211_phy "$device" || return 0 |
| 222 | config_get phy "$device" phy |
| 223 | local i=0 |
| 224 | local macidx=0 |
| 225 | local apidx=0 |
| 226 | fixed="" |
| 227 | local hostapd_ctrl="" |
| 228 | |
| 229 | [ -n "$country" ] && iw reg set "$country" |
| 230 | [ "$channel" = "auto" -o "$channel" = "0" ] || { |
| 231 | fixed=1 |
| 232 | } |
| 233 | |
| 234 | [ -n "$distance" ] && iw phy "$phy" set distance "$distance" |
| 235 | [ -n "$frag" ] && iw phy "$phy" set frag "${frag%%.*}" |
| 236 | [ -n "$rts" ] && iw phy "$phy" set rts "${rts%%.*}" |
| 237 | |
| 238 | export channel fixed |
| 239 | # convert channel to frequency |
| 240 | local freq="$(get_freq "$phy" "${fixed:+$channel}")" |
| 241 | |
| 242 | wifi_fixup_hwmode "$device" "g" |
| 243 | for vif in $vifs; do |
| 244 | while [ -d "/sys/class/net/wlan$i" ]; do |
| 245 | i=$(($i + 1)) |
| 246 | done |
| 247 | |
| 248 | config_get ifname "$vif" ifname |
| 249 | [ -n "$ifname" ] || { |
| 250 | ifname="wlan$i" |
| 251 | } |
| 252 | config_set "$vif" ifname "$ifname" |
| 253 | |
| 254 | config_get mode "$vif" mode |
| 255 | config_get ssid "$vif" ssid |
| 256 | |
| 257 | # It is far easier to delete and create the desired interface |
| 258 | case "$mode" in |
| 259 | adhoc) |
| 260 | iw phy "$phy" interface add "$ifname" type adhoc |
| 261 | ;; |
| 262 | ap) |
| 263 | # Hostapd will handle recreating the interface and |
| 264 | # it's accompanying monitor |
| 265 | apidx="$(($apidx + 1))" |
| 266 | i=$(($i + 1)) |
| 267 | [ "$apidx" -gt 1 ] || iw phy "$phy" interface add "$ifname" type managed |
| 268 | ;; |
| 269 | mesh) |
| 270 | config_get mesh_id "$vif" mesh_id |
| 271 | iw phy "$phy" interface add "$ifname" type mp mesh_id "$mesh_id" |
| 272 | ;; |
| 273 | monitor) |
| 274 | iw phy "$phy" interface add "$ifname" type monitor |
| 275 | ;; |
| 276 | sta) |
| 277 | local wdsflag |
| 278 | config_get_bool wds "$vif" wds 0 |
| 279 | [ "$wds" -gt 0 ] && wdsflag="4addr on" |
| 280 | iw phy "$phy" interface add "$ifname" type managed $wdsflag |
| 281 | config_get_bool powersave "$vif" powersave 0 |
| 282 | [ "$powersave" -gt 0 ] && powersave="on" || powersave="off" |
| 283 | iwconfig "$ifname" power "$powersave" |
| 284 | ;; |
| 285 | esac |
| 286 | |
| 287 | # All interfaces must have unique mac addresses |
| 288 | # which can either be explicitly set in the device |
| 289 | # section, or automatically generated |
| 290 | config_get macaddr "$device" macaddr |
| 291 | local mac_1="${macaddr%%:*}" |
| 292 | local mac_2="${macaddr#*:}" |
| 293 | |
| 294 | config_get vif_mac "$vif" macaddr |
| 295 | [ -n "$vif_mac" ] || { |
| 296 | if [ "$macidx" -gt 0 ]; then |
| 297 | offset="$(( 2 + $macidx * 4 ))" |
| 298 | else |
| 299 | offset="0" |
| 300 | fi |
| 301 | vif_mac="$( printf %02x $((0x$mac_1 + $offset)) ):$mac_2" |
| 302 | macidx="$(($macidx + 1))" |
| 303 | } |
| 304 | [ "$mode" = "ap" ] || ifconfig "$ifname" hw ether "$vif_mac" |
| 305 | config_set "$vif" macaddr "$vif_mac" |
| 306 | |
| 307 | # !! ap !! |
| 308 | # |
| 309 | # ALL ap functionality will be passed to hostapd |
| 310 | # |
| 311 | # !! station !! |
| 312 | # |
| 313 | # ALL station functionality will be passed to wpa_supplicant |
| 314 | # |
| 315 | if [ ! "$mode" = "ap" ]; then |
| 316 | # We attempt to set the channel for all interfaces, although |
| 317 | # mac80211 may not support it or the driver might not yet |
| 318 | # for ap mode this is handled by hostapd |
| 319 | [ -n "$fixed" -a -n "$channel" ] && iw dev "$ifname" set channel "$channel" |
| 320 | fi |
| 321 | |
| 322 | config_get vif_txpower "$vif" txpower |
| 323 | # use vif_txpower (from wifi-iface) to override txpower (from |
| 324 | # wifi-device) if the latter doesn't exist |
| 325 | txpower="${txpower:-$vif_txpower}" |
| 326 | [ -z "$txpower" ] || iw dev "$ifname" set txpower fixed "${txpower%%.*}00" |
| 327 | done |
| 328 | |
| 329 | local start_hostapd= |
| 330 | rm -f /var/run/hostapd-$phy.conf |
| 331 | for vif in $vifs; do |
| 332 | config_get mode "$vif" mode |
| 333 | [ "$mode" = "ap" ] || continue |
| 334 | mac80211_hostapd_setup_bss "$phy" "$vif" |
| 335 | start_hostapd=1 |
| 336 | done |
| 337 | |
| 338 | [ -n "$start_hostapd" ] && { |
| 339 | hostapd -P /var/run/wifi-$phy.pid -B /var/run/hostapd-$phy.conf || { |
| 340 | echo "Failed to start hostapd for $phy" |
| 341 | return |
| 342 | } |
| 343 | sleep 2 |
| 344 | |
| 345 | for vif in $vifs; do |
| 346 | config_get mode "$vif" mode |
| 347 | config_get ifname "$vif" ifname |
| 348 | [ "$mode" = "ap" ] || continue |
| 349 | hostapd_ctrl="${hostapd_ctrl:-/var/run/hostapd-$phy/$ifname}" |
| 350 | mac80211_start_vif "$vif" "$ifname" |
| 351 | done |
| 352 | } |
| 353 | |
| 354 | for vif in $vifs; do |
| 355 | config_get mode "$vif" mode |
| 356 | config_get ifname "$vif" ifname |
| 357 | [ ! "$mode" = "ap" ] || continue |
| 358 | ifconfig "$ifname" up |
| 359 | |
| 360 | if [ ! "$mode" = "ap" ]; then |
| 361 | ifconfig "$ifname" up |
| 362 | case "$mode" in |
| 363 | adhoc) |
| 364 | config_get bssid "$vif" bssid |
| 365 | config_get ssid "$vif" ssid |
| 366 | config_get mcast_rate "$vif" mcast_rate |
| 367 | local mcval="" |
| 368 | [ -n "$mcast_rate" ] && { |
| 369 | mcval="$(($mcast_rate / 1000))" |
| 370 | mcsub="$(( ($mcast_rate / 100) % 10 ))" |
| 371 | [ "$mcsub" -gt 0 ] && mcval="$mcval.$mcsub" |
| 372 | } |
| 373 | iw dev "$ifname" ibss join "$ssid" $freq ${fixed:+fixed-freq} $bssid ${mcval:+mcast-rate $mcval} |
| 374 | ;; |
| 375 | sta) |
| 376 | if eval "type wpa_supplicant_setup_vif" 2>/dev/null >/dev/null; then |
| 377 | wpa_supplicant_setup_vif "$vif" nl80211 "${hostapd_ctrl:+-H $hostapd_ctrl}" || { |
| 378 | echo "enable_mac80211($device): Failed to set up wpa_supplicant for interface $ifname" >&2 |
| 379 | # make sure this wifi interface won't accidentally stay open without encryption |
| 380 | ifconfig "$ifname" down |
| 381 | continue |
| 382 | } |
| 383 | fi |
| 384 | ;; |
| 385 | esac |
| 386 | mac80211_start_vif "$vif" "$ifname" |
| 387 | fi |
| 388 | done |
| 389 | |
| 390 | } |
| 391 | |
| 392 | |
| 393 | check_device() { |
| 394 | config_get phy "$1" phy |
| 395 | [ -z "$phy" ] && { |
| 396 | find_mac80211_phy "$1" >/dev/null || return 0 |
| 397 | config_get phy "$1" phy |
| 398 | } |
| 399 | [ "$phy" = "$dev" ] && found=1 |
| 400 | } |
| 401 | |
| 402 | detect_mac80211() { |
| 403 | devidx=0 |
| 404 | config_load wireless |
| 405 | while :; do |
| 406 | config_get type "radio$devidx" type |
| 407 | [ -n "$type" ] || break |
| 408 | devidx=$(($devidx + 1)) |
| 409 | done |
| 410 | for dev in $(ls /sys/class/ieee80211); do |
| 411 | found=0 |
| 412 | config_foreach check_device wifi-device |
| 413 | [ "$found" -gt 0 ] && continue |
| 414 | |
| 415 | mode_11n="" |
| 416 | mode_band="g" |
| 417 | channel="11" |
| 418 | ht_cap=0 |
| 419 | for cap in $(iw phy "$dev" info | grep 'Capabilities:' | cut -d: -f2); do |
| 420 | ht_cap="$(($ht_cap | $cap))" |
| 421 | done |
| 422 | ht_capab=""; |
| 423 | [ "$ht_cap" -gt 0 ] && { |
| 424 | mode_11n="n" |
| 425 | append ht_capab " option htmode HT20" "$N" |
| 426 | |
| 427 | list=" list ht_capab" |
| 428 | [ "$(($ht_cap & 1))" -eq 1 ] && append ht_capab "$list LDPC" "$N" |
| 429 | [ "$(($ht_cap & 16))" -eq 16 ] && append ht_capab "$list GF" "$N" |
| 430 | [ "$(($ht_cap & 32))" -eq 32 ] && append ht_capab "$list SHORT-GI-20" "$N" |
| 431 | [ "$(($ht_cap & 64))" -eq 64 ] && append ht_capab "$list SHORT-GI-40" "$N" |
| 432 | [ "$(($ht_cap & 128))" -eq 128 ] && append ht_capab "$list TX-STBC" "$N" |
| 433 | [ "$(($ht_cap & 768))" -eq 256 ] && append ht_capab "$list RX-STBC1" "$N" |
| 434 | [ "$(($ht_cap & 768))" -eq 512 ] && append ht_capab "$list RX-STBC12" "$N" |
| 435 | [ "$(($ht_cap & 768))" -eq 768 ] && append ht_capab "$list RX-STBC123" "$N" |
| 436 | [ "$(($ht_cap & 4096))" -eq 4096 ] && append ht_capab "$list DSSS_CCK-40" "$N" |
| 437 | } |
| 438 | iw phy "$dev" info | grep -q '2412 MHz' || { mode_band="a"; channel="36"; } |
| 439 | |
| 440 | cat <<EOF |
| 441 | config wifi-device radio$devidx |
| 442 | option type mac80211 |
| 443 | option channel ${channel} |
| 444 | option macaddr $(cat /sys/class/ieee80211/${dev}/macaddress) |
| 445 | option hwmode 11${mode_11n}${mode_band} |
| 446 | $ht_capab |
| 447 | # REMOVE THIS LINE TO ENABLE WIFI: |
| 448 | option disabled 1 |
| 449 | |
| 450 | config wifi-iface |
| 451 | option device radio$devidx |
| 452 | option network lan |
| 453 | option mode ap |
| 454 | option ssid OpenWrt |
| 455 | option encryption none |
| 456 | |
| 457 | EOF |
| 458 | devidx=$(($devidx + 1)) |
| 459 | done |
| 460 | } |
| 461 | |
| 462 | |